首页 > 解决方案 > 在成功标志之前的列表中查找完整短语

问题描述

代码:

# function - send a get request to each url
def send_get_request(link, search_for):
    try:
        html = send_request = requests.get(link)
        for i in search_for:
            if any(i in html.text for i in search_for):
                return link
            else:
                return False
    except Exception as e:
        print("Error: " + str(e))

# search for any of these items
search_for = ['about me', 'home page']

在我的search_for列表项中查找时,如果找到关于它的词,则将其标记为成功。我需要找到与homehome page相同的关于我的完整单词。

标签: pythonpython-3.x

解决方案


利用re.findall

import re

search_for = ['about me', 'home page', 'home']

def send_get_request(link, search_for):
   try:
       html = requests.get(link)
   except requests.exceptions.RequestException as e:
       print("Error: {}".format(e))

   if re.findall('|'.join(search_for), html.text):
       return html.text
   else:
       return False

推荐阅读