我試圖從我的 Google API 搜索結(jié)果中排除某些鏈接。我正在嘗試使用從 links_to_exclude 列表中提取的正則表達(dá)式。這種方法仍然輸出我不需要的鏈接。如何使用正則表達(dá)式排除這些鏈接?links_to_exclude = ['cnn.com', 'nytimes.com']for item in search_terms:results = google_search(item, api_key, cse_id, num=1)for result in results: rtn_link = result.get('link') for link in links_to_exclude: regex = '((http[s]?|ftp):\/)?\/?([^:\/\s]+)?({})\/([^\/]+)'.format(link) if re.search(regex, rtn_link): continue else: pprint.pprint(result.get('link'))
添加回答
舉報(bào)
0/150
提交
取消