简体   繁体   中英

Getting value from page with BeautifulSoup

I have the following page structure:

       <tr class="small data-row" bgcolor="#f9f9f9">.</tr> 
           <td class="stats1" align="right">0</td>
           <td class="stats1" align="right">0</td>
           <td class="stats1" align="right">0</td>
           <td class="stats1 stats-dash" align="right">-</td>
        .
        .
        .
       <tr class="small data-row" bgcolor="#ffffff">.</tr> 
       <tr class="small data-row" bgcolor="#f9f9f9">.</tr>    
       <tr class="small" bgcolor="#eff6ef">.</tr>
            <td class="stats1" align="right">215</td>
            <td class="stats1" align="right">183</td>
            <td class="stats1" align="right">0</td>
            <td class="stats1 stats-dash" align="right">-</td>
       </tr>

I would like to get this second value == 183 , but I am not sure how to do it. I tried in that way:

content = driver.page_source
soup = BeautifulSoup(content)

for elm in soup.select(".stats1"):
    val=elm.get("align")

and the output is:

right
<td align="right" class="stats1">215</td>

if I got 183 instead of 215 I could use.split, but in this case I get only this first value.

.select() will return a list of elements. Just call that element by index:

from bs4 import BeautifulSoup

html = '''<tr class="small data-row" bgcolor="#f9f9f9">.</tr>    
       <tr class="small" bgcolor="#ffffff">.</tr>
            <td class="stats1" align="right">215</td>
            <td class="stats1" align="right">183</td>
            <td class="stats1" align="right">0</td>
            <td class="stats1 stats-dash" align="right">-</td>
       </tr>'''
       
soup = BeautifulSoup(html, 'html.parser')
elm = soup.select(".stats1")[1]

Output:

print(elm.text)
183

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM