user17244269
user17244269

Reputation:

How do I get data from a div in python?

I'm writing a python script that should return a text from a div. The html looks like this:

<div id="bodyContent" class="vector-body">
    <div id="siteSub" class="noprint">From Wikipedia, the free encyclopedia</div>

So my script should return: "From Wikipedia, the free encyclopedia", but it returns nothing

source = requests.get("https://en.wikipedia.org/wiki/2021_Osaka_building_fire").text

soup = BeautifulSoup(source, 'lxml')

print(soup.select_one("div[class*=noprint]").text)

What am I doing wrong ?

Upvotes: 0

Views: 46

Answers (1)

Gui
Gui

Reputation: 175

The problem is there's other div.noprint's in the page, and the 'one' it caught is empty

An alternative is to find out, by id:

print(soup.select_one("div[id*=siteSub]").text)

Upvotes: 2

Related Questions