【问题标题】:Webscraping from wikipedia and uploading into pandas [closed]从维基百科抓取网页并上传到 pandas [关闭]
【发布时间】: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


    【解决方案1】:

    在这种情况下,直接将页面加载到 pandas 中会更容易:

    tables = pd.read_html('https://en.wikipedia.org/wiki/List_of_Test_cricket_triple_centuries')
    tables[1]
    

    输出是您要查找的表格。只需使用标准 pandas 方法删除不必要的列。

    【讨论】:

    • 谢谢杰克。如果您或任何人能够提供代码只是为了使用 for 循环获取分数列,这对我的学习来说将是很棒的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 2016-09-08
    • 2020-07-20
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2022-01-10
    • 2019-07-20
    相关资源
    最近更新 更多