mHelpMe
mHelpMe

Reputation: 6668

beautiful soup find all not finding div

I am downloading some Spanish verbs & their conjugations to improve my Spanish.

On the web page (link here) under the verb at the top their is the English translation. However using the code below is not working, I'm not sure why though? It returns None, why, I can see it on the web page?

URL = 'https://www.linguasorb.com/spanish/verbs/conjugation/trabajar
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
res = soup.find_all('div', class_='l1-infinitive')

Upvotes: 0

Views: 816

Answers (1)

Le Minaw
Le Minaw

Reputation: 885

There's nothing wrong with this code.

import requests
from bs4 import BeautifulSoup as bs

URL = 'https://www.linguasorb.com/spanish/verbs/conjugation/trabajar'

page = requests.get(URL)
soup = bs(page.content, 'html.parser')
res = soup.find_all('div', class_='l1-infinitive')
print(res)

Outputs [<div class="l1-infinitive">to work</div>] on my machine.

Edit: a few more details about what res is

res is an instance of bs4.element.ResultSet. You can manipulate it like a list, thanks to duck typing.

res[0] is an instance of bs4.element.Tag. print(res[0]) outputs a string representing the tag and its contents, because of how the Tag class implements the __str__ method.

BeautifulSoup gives you various possibilities to extract strings from a Tag elements. In this particular case, res[0].text returns the string "to work", which I think is what's you're looking for.

res[0].string returns an instance of bs4.element.NavigableString. More info about them in the docs.

Upvotes: 1

Related Questions