Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrape a class within a class

I want to scrape class_="href" with in the class_="_e4d". Basically looking to scrape a class within a class using 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)

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

I now want to scrape the href result. I am not sure what the next step would be. Thanks for the help.

Note: I replaced <> with {} since it was not showing up as html script

like image 295
Mwspencer Avatar asked Jun 06 '26 15:06

Mwspencer


1 Answers

You can actually find this inner a element in one go with a CSS selector:

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

p._e4b a[href] would locate all a elements having the href attribute inside the p elements having _e4b class.

like image 51
alecxe Avatar answered Jun 08 '26 04:06

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!