span元素中的小文本过滤

2024-10-04 03:27:11 发布

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

我正在寻找从谷歌搜索结果过滤文本(描述)。它包含在一个元素中。 我的工作如下:

result_div.find('span', attrs={'class': 'VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc'})

其中result_div = soup.find_all('div', attrs={'class': 'g'})

但它返回一个空值

我怎样才能得到这个

我试过的代码:

driver.get('https://www.google.com')
s = driver.find_element_by_name('q')
s.send_keys("stackoverflow")
s.submit()
soup = BeautifulSoup(driver.page_source,'lxml')
result_div.find('span', attrs={'class': 'VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc'})

Tags: 文本div元素driverresultfindattrsclass
1条回答
网友
1楼 · 发布于 2024-10-04 03:27:11

有多个名为g的类,但并非所有类都包含嵌套类VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc。 由于您只需要描述,只需直接搜索类VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome()

driver.get("https://www.google.com")
s = driver.find_element_by_name("q")
s.send_keys("stackoverflow")
s.submit()
soup = BeautifulSoup(driver.page_source, "lxml")


for tag in soup.find_all("div", class_="VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc"):
    print(tag.text)

输出:

Stack Overflow is the largest, most trusted online community for developers to learn, share​ ​their programming ​knowledge, and build their careers.
Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network, created in ...
The Stack Overflow Podcast is a weekly conversation about working in software development, learning to code, and the art and culture of computer programming.
The Stack Overflow podcast is a frank and funny conversation about what it means to work in software and how code is reshaping our world. As it celebrates its ...
Stack Overflow is the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. More than 50 million ...
5 days ago — For the past five years, the Go user survey has consistently identified Stack Overflow as the #1 place users go to find answers to their Go ...

相关问题 更多 >