python - 如何在python中使用request和beautifulsoup在网页中搜索多个预定义字符串
问题描述
我想在页面中搜索包含预定义模式的多个字符串。目前,我的代码似乎有问题。
import requests, re
url = "https://bscscan.com/address/0x88c20beda907dbc60c56b71b102a133c1b29b053#code"
queries = ["t.me", "twitter", "www."]
r = requests.get(url)
for q in queries:
print (q)
if q.startswith(tuple(queries)):
print(q, 'Found')
else:
print(q, 'Not Found')
电流输出:
t.me
t.me Found
twitter
twitter Found
www.
www. Found
想要的输出:
www.shibuttinu.com - Found
https://t.me/Shibuttinu - Found
twitter - not found
解决方案
这是一个关于如何使用模块的示例,它甚至可能是解决方案。对不起,但不知道你真正想要什么......但我希望它对你有帮助
import requests
from bs4 import BeautifulSoup
res = requests.get(url)
soup = BeautifulSoup(res.text)
queries = ["t.me", "twitter", "www."]
results = []
for q in queries:
# match condition here, in case change it
results += soup.find_all(lambda t: q in str(t.string), string=True)
for r in results:
print(r)
推荐阅读
- apache-superset - 如何在 Superset 中设置当前季度?
- haskell - Haskell RankNTypes - 函数域的限制
- wpf - (后面的代码)如何使用 WPFtoolkit 绑定多个系列的柱形图?
- android - Fragment中RecyclerView的Android单元测试用例
- ruby-on-rails-5 - 未定义的方法“和”用于#
我在让查询正常工作时遇到问题。
我实际上是在尝试编写类似于以下 SQL 的内容,将文字
5
替换为变量:SELECT * FROM "my_table" WHERE 5 BETWEEN "my_table"."minimum" AND "my_table"."maximum"
- scala - 在scala中提取分隔符后的单词
- pandas - 如何在熊猫的1列中转换多列
- java - Jackson @JsonTypeInfo 在 JSON 反序列化期间使该字段无法读取
- swift - 为什么生成的头文件中导入失败?
- reactjs - 有没有办法在 React 中隐藏密码输入的值?