3 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用 CSS 選擇器nth-of-type(n)。它適用于兩個(gè)鏈接:
import requests
from bs4 import BeautifulSoup
url = "https://system.gotsport.com/org_event/events/1271/schedules?age=19&gender=m"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
for tag in soup.select(".small-margin-bottom td:nth-of-type(1)"):
print(tag.text.strip())
輸出:
OCYS
FL Rush
Jacksonville FC
Atlanta United
SSA
...
...
Real Salt Lake U19
Real Colorado
Empire United Soccer Academy

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
每個(gè)括號對應(yīng)一個(gè)“面板”,每個(gè)面板有兩行,第一行包含比賽表中所有球隊(duì)的第一個(gè)表。
def main():
import requests
from bs4 import BeautifulSoup
url = "https://system.gotsport.com/org_event/events/1271/schedules?age=19&gender=m"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
for panel in soup.find_all("div", {"class": "panel-body"}):
for row in panel.find("tbody").find_all("tr"):
print(row.find("td").text.strip())
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
輸出:
OCYS
FL Rush
Jacksonville FC
Atlanta United
SSA
Miami Rush Kendall SC
IMG
Tampa Bay United
Weston FC
Chargers SC
South Florida FA
Solar SC
RISE SC
...

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
我認(rèn)為問題出在表的標(biāo)題上,它包含th元素而不是td元素。當(dāng)您嘗試從空列表中檢索第一個(gè)元素時(shí),它會(huì)導(dǎo)致范圍索引錯(cuò)誤。嘗試添加長度檢查td:
for row in rows:
team = row.find_all('td')
if(len(team) > 0):
teamName = team[0].text.strip()
print(teamName)
它應(yīng)該打印出團(tuán)隊(duì)名稱。
添加回答
舉報(bào)