Skip to content Skip to sidebar Skip to footer

Parsing Css Selector With Beautifulsoup

I am using the CSS selector to scrape data from the web with the beautifulsoup 4 module. see sample code: # pull website res = requests.get('https://dailystoic.com/epictetus/')

Solution 1:

use :nth-of-type() instead of nth-child().

import bs4, requests
res = requests.get('https://dailystoic.com/epictetus/')
soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('body > div.wrap.container > div > main > article > div.entry-content > p:nth-of-type(1) > em > a:nth-of-type(3)')
print(elems[0].text)

.text get what the hyperlink says - the link text. If you wanted the URL, you'd do: elems[0].attrs['href']

Output:

Epictetus

Post a Comment for "Parsing Css Selector With Beautifulsoup"