【发布时间】:2018-08-22 15:14:33
【问题描述】:
我正在尝试从我的 Google API 搜索结果中排除某些链接。我正在尝试使用从 links_to_exclude 列表中提取的正则表达式。这种方法仍然会输出我不想要的链接。
返回的一些链接:
https://money.cnn.com/2018/08/21/technology/facebook-disinformation-iran-russia/index.html
如何使用正则表达式排除这些链接?
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'))
【问题讨论】:
标签: python regex python-3.x list google-api