如何通过以下链接网上搜索反映德州游骑兵赛季的桌子?我正在使用BeautifulSoup4和html.parser

问题描述 投票:0回答:1

这是我试过的:

import requests
website_url = "https://en.wikipedia.org/wiki/List_of_Texas_Rangers_seasons"
url = requests.get(website_url).text
from bs4 import BeautifulSoup
soup = BeautifulSoup(website_url,'html.parser')

# Selecting the table
table_classes = {"class":"wikitable plainrowheaders"}
rel_table = soup.find_all('table',table_classes)

我不知道如何进一步。我确实检查了元素,看起来标题和href都是动态的,包含年份字段。同时,它还包含华盛顿参议员的表格。我将不胜感激任何帮助!谢谢!

python web-scraping
1个回答
0
投票
from bs4 import BeautifulSoup
import requests

url = 'https://en.wikipedia.org/wiki/List_of_Texas_Rangers_seasons'
r = requests.get(url)

soup = BeautifulSoup(r.text,'lxml')

#method 1
for row in soup.select('table.plainrowheaders tr')[14:]:
    for cell in row.select('td'):
        print(cell.text.strip(), end=' ')
    print()

#method 2
for row in soup.select('table.plainrowheaders tr')[14:]:
    print(row.get_text(strip=True, separator=' '))
© www.soinside.com 2019 - 2024. All rights reserved.