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

2024-04-26 22:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

# 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']

在“我的搜索”中查找列表项时,如果找到“关于的一词,则将其标记为成功。我需要找到关于我的完整单词,就像主页和主页一样。你知道吗


Tags: to代码insendforsearchgetreturn
1条回答
网友
1楼 · 发布于 2024-04-26 22:21:55

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

相关问题 更多 >