如何使用请求和 python 中的 beautifulsoup 在网页中搜索多个预定义字符串
How to search multiple predefined string in a webpage using request and beautifulsoup in python
我想在一个页面中搜索包含预定义模式的多个字符串。目前,我的代码似乎有问题。
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)
我想在一个页面中搜索包含预定义模式的多个字符串。目前,我的代码似乎有问题。
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)