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']
在我的search_for列表项中查找时,如果找到关于它的词,则将其标记为成功。我需要找到与home和home page相同的关于我的完整单词。
解决方案
利用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
推荐阅读
- java - 在 Java 套接字中将对象从服务器发送到客户端
- c - 如何在没有通用头文件的情况下共享数组类型定义?
- mysql - 为什么这种“嵌套连接”适用于 PDO 而不适用于 MySql cli?
- javascript - 闪亮的数据表工具提示/弹出框不适用于分页
- rust - 有没有办法使用地图中元素的值来更新另一个没有内部可变性的元素?
- java - 分而治之算法 - 结果 NullPointerException
- mysql - 具有两个条件的 Laravel 搜索查询
- asp.net - OData - $expand 中的 $top 不会在生成的 EF 查询中插入“TOP”
- asp.net-core - 从 Swagger 生成 .NET 客户端
- matlab - matlab并行计算中的变量通信消耗