问题
Hi I am trying to write a program that scraps a URL and if the scrap data contains a particular string do something how can i use beautiful soup to achieve this
import requests
from bs4 import BeautifulSoup
data = requests.get('https://www.google.com',verify=False)
soup= BeautifulSoup(data.string,'html.parser')
for inp in soup.find_all('input'):
if inp == "Google Search":
print ("found")
else:
print ("nothing")
回答1:
Your inp is a html object. You must use get_text() function
import requests
from bs4 import BeautifulSoup
data = requests.get('https://www.google.com',verify=False)
soup= BeautifulSoup(data.string,'html.parser')
for inp in soup.find_all('input'):
if inp.get_text() == "Google Search":
print ("found")
else:
print ("nothing")
回答2:
verify = False, disables certificate verification. This is a security matter. Especially if you are inside a company network this is dangerous because it opens the possibility for a man in the middle attack. You should use proper certificate authorization.
来源:https://stackoverflow.com/questions/53133736/how-to-use-beautifulsoup-to-check-if-a-string-exists