Parse BS4 td>class>text

Viewed 40

how parse "Level" and "Tributes" by BS4 python?

def return_42():
 url = "https://example.com/"
 server = "&serv=server4"
 nickname = 'torn'
 headers = {
    "User-agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
 urls = (url+nickname+server)
 html = requests.get(urls, headers=headers)
 soup = BeautifulSoup(html.text, 'html.parser')
 for row in soup.select("table.sblock tbody tr"):
    key, value = [td.get_text(strip=True) for td in row.select("td")]
    return key
print(return_42())

i get None in Terminal

2 Answers

Loop through all the tr in the tbody, and get the text of the first td.

tables = soup.findall('table',class_="sblock")
for table in tables:
    for row in table.find('tbody').findall('tr'):
        print(row.find('tr').string)

Select all rows (<tr>) and then extract key (first <td>), value (second <td>) from the row:

from bs4 import BeautifulSoup


html = """\
<table border="0" class="sblock" width="100%">
<tbody>
<tr>
    <td align="left" class="sblock">Level</td>
    <td align="left" class="sblock"><div class="title">400</div></td>
</tr>
<tr>
    <td align="left" class="sblock">Tributes</td>
    <td align="left" class="sblock"><div class="title">52</div></td>
</tr>
</tbody></table>"""

soup = BeautifulSoup(html, "html.parser")

for row in soup.select("table.sblock tbody tr"):
    key, value = [td.get_text(strip=True) for td in row.select("td")]
    print(key, value)

Prints:

Level 400
Tributes 52

EDIT: With updated question:

def return_42():

    url = "https://mu.bless.gs/en/index.php?page=info&act=getchar&name="
    server = "&serv=server4"
    nickname = "torn"
    headers = {
        "User-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
    }
    urls = url + nickname + server
    html = requests.get(urls, headers=headers)
    soup = BeautifulSoup(html.text, "html5lib")  # <-- !!! important !!! use "html5lib"

    out = {}
    for row in soup.select("table.sblock:has(table) tbody tr"):
        tds = [td.get_text(strip=True) for td in row.select("td")][-2:]
        if len(tds) != 2:
            continue
        out[tds[0]] = tds[1]
    return out


print(return_42())

Prints:

{
    "Character": "Torn",
    "Guild": "IOPGl",
    "Master": "Krju4ok",
    "Guild Buff": "6,725",
    "Class": "Lord Emperor",
    "Level": "400",
    "Tributes": "52",
    "Resets": "36",
    "PK level": "Commoner (0)",
    "Location": "Silent Grounds (196 x 70)",
    "Strength": "18,026",
    "Agility": "16,020",
    "Vitality": "10,553",
    "Energy": "30,020",
    "Command": "600",
    "Bought stats": "2000",
    "Achiev. points": "125",
    "Buff level": "125",
    "Equipment": "0",
    "Completed Quests": "380",
    "ET level": "23",
    "Server": "Origin-GS",
    "Status": "Offline",
}
Related