在类中抓取一个类

Mws*_*cer 3 python selenium webdriver beautifulsoup bs4

我想class_="href"class_="_e4d". 基本上是想使用 BeautifulSoup 在一个类中抓取一个类。

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)
Run Code Online (Sandbox Code Playgroud)

结果:markup_type=markup_type)) p class="_e4b"}{a href="/search?...a}{/p}

我现在想抓取 href 结果。我不确定下一步会是什么。谢谢您的帮助。

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

ale*_*cxe 5

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

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

p._e4b a[href]将在具有类的元素内定位a具有该href属性的所有元素。p_e4b