How to use beautifulsoup to check if a string exists

南笙酒味 提交于 2021-02-10 06:33:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!