猿问

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

我试图从我的 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'))


温温酱
浏览 182回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答