首页 > 解决方案 > 如何在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

标签: pythonpython-3.xbeautifulsouprequestwebrequest

解决方案


这是一个关于如何使用模块的示例,它甚至可能是解决方案。对不起,但不知道你真正想要什么......但我希望它对你有帮助

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)

推荐阅读