为了账号安全,请及时绑定邮箱和手机立即绑定

使用正则表达式排除搜索结果中的 href 链接

使用正则表达式排除搜索结果中的 href 链接

温温酱 2021-05-30 00:14:23
我试图从我的 Google API 搜索结果中排除某些链接。我正在尝试使用从 links_to_exclude 列表中提取的正则表达式。这种方法仍然输出我不需要的链接。如何使用正则表达式排除这些链接?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'))
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号