I wanted to search a page for multiple strings that contains a predefined pattern. Currently, my code seems problematic.
JavaScript
x
13
13
1
import requests, re
2
url = "https://bscscan.com/address/0x88c20beda907dbc60c56b71b102a133c1b29b053#code"
3
4
queries = ["t.me", "twitter", "www."]
5
6
r = requests.get(url)
7
for q in queries:
8
print (q)
9
if q.startswith(tuple(queries)):
10
print(q, 'Found')
11
else:
12
print(q, 'Not Found')
13
Current Output:
JavaScript
1
7
1
t.me
2
t.me Found
3
twitter
4
twitter Found
5
www.
6
www. Found
7
Wanted Output:
JavaScript
1
4
1
www.shibuttinu.com - Found
2
https://t.me/Shibuttinu - Found
3
twitter - not found
4
Advertisement
Answer
Here a sample example on how use the modules, it could be even the solution. Sorry but no ideas of what do you really want… but I hope it helps you anyway
JavaScript
1
15
15
1
import requests
2
from bs4 import BeautifulSoup
3
4
res = requests.get(url)
5
soup = BeautifulSoup(res.text)
6
7
queries = ["t.me", "twitter", "www."]
8
results = []
9
for q in queries:
10
# match condition here, in case change it
11
results += soup.find_all(lambda t: q in str(t.string), string=True)
12
13
for r in results:
14
print(r)
15