Lopez Nlak Lopez Nlak - 1 year ago 41
Python Question

Scraping tabular data with Python-BeautifulSoup

Can't figure out how to scrape the first table data instead of both.

<tr>
<td>WheelDust
</td>
<td>A large puff of barely visible brown dust
</td></tr>


I only want WheelDust but instead I get WheelDust and A large puff of barely visible brown dust

import requests
from bs4 import BeautifulSoup


r = requests.get("https://wiki.garrysmod.com/page/Effects")

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

for td in soup.findAll("table"):
#--print(td)
for a in td.findAll("tr"):
print(a.text)

Answer Source

I'm still not sure what you're asking, but I believe you're saying that you want to access the and only the first , correct? If that's the case, would this not work? I'd try it but it says I don't have access to the website.

import requests
from bs4 import BeautifulSoup


r = requests.get("https://wiki.garrysmod.com/page/Effects")

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

for td in soup.findAll("table"):
    #--print(td)
    for a in td.findAll("tr"):
        print(a.find('td'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download