Web Scraping数据可视化

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

我正在努力捕捉 AND 脚本完成后,以表格的形式呈现数据。我使用的网站是 http:/en.wikipedia.orgwikiList_ofall-time_NFL_win-loss_records。 而逻辑是这样的工作。

  1. 我运行这个命令,它打开的网址是:
  2. 然后,我进入URL http:/en.wikipedia.orgwikiList_ofall-time_NFL_win-loss_records。
  3. 我继续从Tablechart中复制任何选定的行和列。
  4. 然后我回到我的IDE(Jupyter Notebook),它就会把采集到的数据吐出来。

我可以选择该网页上的数据,然后用光标复制,方法是突出显示并选择 "复制"。然后,它将会吐出所有我选择并复制到剪贴板的数据。

到目前为止,我写的脚本只捕捉数据,然后按原样吐出(未格式化)。

问题: 我希望我采集的数据能以表格的形式呈现。之后 我已经完成了选择,并将其复制到剪贴板中。

我意识到,我可能需要为我捕获的数据编写逻辑,然后进行格式化。实现这个目标的最佳方法是什么?

下面是我目前写的代码。

这是我的代码

import numpy as np
Import pandas as pd
from pandas import Series, Dataframe
website='http://en.wikipedia.org/wiki/NFL_win_loss_records'
web browser.open(website)
nfl_frame= pd.read_clipboard(Sep='\t')
nfl_frame
python numpy web-scraping formatting data-visualization
1个回答
1
投票

你可以直接将你的数据读到DataFrame中,用 pandas.read_html

import pandas as pd

WIKI_URL = 'http://en.wikipedia.org/wiki/List_of_all-time_NFL_win-loss_records'
df = pd.read_html(WIKI_URL,header=0)[1] 

df.head() # in jupyter or print(df.head()) to show a table with first 5 rows

作为 pd.read_html 返回一个列表。其中有该HTMLURL中的表。我将header设置为第一生,并选择了列表中的第二个元素,也就是你要找的表。

© www.soinside.com 2019 - 2024. All rights reserved.