在成功标记之前的列表中查找完整短语

时间:2019-03-11 20:59:21

标签: python python-3.x

代码:

# 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 列表项中查找时,如果找到关于一词,则将其标记为成功。我需要找到完整的单词,该单词与关于我首页首页相同。

1 个答案:

答案 0 :(得分:0)

使用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