Alau0828
Alau0828

Reputation: 75

Python - how to get table id

The HTML is shown as below. How can i get the id="table_1880381"?

<div class="match" id="table_1880381"><div class="m_info"><div class="game" style="background-color:#669900;"><a href="http://info.win007.com/cn/subleague.aspx?sclassid=140" target="_blank">墨西聯</a><br/><span id="mt_1880381">7-31 02:30</span> </div><div class="time" id="time_1880381"><font color="red">完</font></div><div class="home" id="home_1880381"><div class="teamInfo"><div class="match" id="table_1851236"><div class="m_info"><div class="game" style="background-color:#64ba1e;"><a href="http://info.win007.com/cn/subleague.aspx?sclassid=772" target="_blank">烏茲超</a><br><span id="mt_1851236">8-18 20:00</span> </div><div class="time" id="time_1851236"><font color="red">完</font></div><div class="home" id="home_1851236"><div class="teamInfo"> <a href="javascript:Panlu(1851236)"><b>克孜勒庫姆</b></a>(中)[14]</div>

    container3 = soup.findAll("div", {"match": "id"})
    print(container3)

When i run the above code, the result should be []. As i want to the result should be 1880381 Anyone mistake on the code?

Upvotes: 0

Views: 317

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24928

Your html is confusing, but there are a number of ways to get to where you want to go.

Try something like:

soup.select_one('div.home').attrs['id'].split('_')[1]

or

soup.select_one('div.time').attrs['id'].split('_')[1]

Output, in either case:

'1880381'

For your modified html:

for item in soup.select('div.home'):
    print(item['id'].split('_')[1])

Output:

1880381
1851236

Upvotes: 1

Related Questions