使用 Python-BeautifulSoup 抓取表格数据

Scraping tabular data with Python-BeautifulSoup

无法弄清楚如何抓取第一个 table 数据而不是两者。

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

我只想要 WheelDust,但我得到了 WheelDust 和一大团几乎看不见的棕色灰尘

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)

我仍然不确定你在问什么,但我相信你是说你想访问并且只访问第一个,对吗?如果是这样,这行不通吗?我想试试,但它说我无法访问该网站。

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'))

也试试这个。它将为您提供 table.

中的所有数据
import requests ; from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get("https://wiki.garrysmod.com/page/Effects").text, "html.parser")

table = soup.findAll('table', attrs={'class':'wikitable'})[0] # Changing the index number will give you whichever table you like
list_of_rows = [[t_data.text for t_data in item.findAll('td')]
                for item in table.findAll('tr')]

for data in list_of_rows:
    print(data)