Skip to main content
 首页 » 编程设计

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

2024年12月31日19mate10pro

您好,我正在尝试编写一个程序来抓取 URL,如果抓取数据包含特定字符串,我该如何使用漂亮的汤来实现此目标

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

请您参考如下方法:

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