【发布时间】:2020-09-09 23:10:49
【问题描述】:
用于抓取的维基页面:https://en.wikipedia.org/wiki/List_of_Test_cricket_triple_centuries
我想要得分,击球手,支持,反对和地面列。我已经采取了单独提取每一列然后将它们组合成熊猫数据框的方法。
我希望得到一些帮助:
- 我可以在其他 for 循环之上添加代码,以便能够提取分数列。
- 上传到 pandas,这样我就可以将所有数据放在一个表中。
刚刚开始我的 Python 之旅,非常感谢所有帮助!
代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
wiki = "https://en.wikipedia.org/wiki/List_of_Test_cricket_triple_centuries"
website_url = requests.get(wiki).text
soup = BeautifulSoup(website_url, "lxml")
my_table = soup.find("table", {"class":"wikitable sortable"})
score = [] #Need assistance extracting this column #
batsmen = []
team = [] #'For' column in Wiki#
against = []
ground = []
# Would like to add the code to extract the Score column here #
for row in my_table.find_all("tr")[1:]:
batsmen_cell = row.find_all("a")[0]
batsmen.append(batsmen_cell.text)
for row in my_table.find_all("tr")[1:]:
team_cell = row.find_all("a")[1]
team.append(team_cell.text)
for row in my_table.find_all("tr")[1:]:
against_cell = row.find_all("a")[2]
against.append(against_cell.text)
for row in my_table.find_all("tr")[1:]:
ground_cell = row.find_all("a")[5]
ground.append(ground_cell.text)
data = [batsmen, team, against, ground]
df = pd.DataFrame(data, columns = ["Batsmen", "For", "Against", "Ground"])
print(df)
【问题讨论】:
标签: python pandas dataframe beautifulsoup