在一个类中刮一个类

2024-04-29 01:17:51 发布

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

我想用class_="_e4d"中的class_="href"。基本上是想用beauthoulsoup在一个类中创建一个类。在

from bs4 import BeautifulSoup
import selenium.webdriver as webdriver

url = ("https://www.google.com/search?...")

def get_related_search(url):
    driver = webdriver.Chrome("C:\\Users\\John\\bin\\chromedriver.exe")
    driver.get(url)
    soup = BeautifulSoup(driver.page_source)
    relate_result = soup.find_all("p", class_="_e4b")
    return relate_result[0]

relate_url = get_related_search(url)
print(relate_url)

结果:标记类型=标记类型) p^{cl1}$

现在我要刮取href结果。我不知道下一步会是什么。谢谢你的帮助。在

注意:我用{}替换了<;>;,因为它没有显示为html脚本


Tags: 标记importurl类型searchgetdriverresult
1条回答
网友
1楼 · 发布于 2024-04-29 01:17:51

实际上,您可以使用一个CSS selector一次性找到这个内部a元素:

links = soup.select("p._e4b a[href]")
for link in links:
    print(link['href'])

p._e4b a[href]将在具有_e4b类的p元素内找到所有具有href属性的{}元素。在

相关问题 更多 >