如何使用beautifulsoup检查字符串是否存在

2024-06-09 15:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我试图写一个程序,剪贴一个网址,如果废料数据包含一个特定的字符串做什么,我如何使用美丽的汤来实现这一点

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")

Tags: 数据字符串fromimport程序datarequests网址
2条回答

您的inp是一个html对象。必须使用get_text()函数

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")

verify=False,禁用证书验证。这是安全问题。尤其是如果你在一个公司的网络中,这是很危险的,因为这可能会导致中间人的攻击。您应该使用正确的证书授权。在

相关问题 更多 >