我正在努力捕捉 AND 脚本完成后,以表格的形式呈现数据。我使用的网站是 http:/en.wikipedia.orgwikiList_ofall-time_NFL_win-loss_records。 而逻辑是这样的工作。
我可以选择该网页上的数据,然后用光标复制,方法是突出显示并选择 "复制"。然后,它将会吐出所有我选择并复制到剪贴板的数据。
到目前为止,我写的脚本只捕捉数据,然后按原样吐出(未格式化)。
问题: 我希望我采集的数据能以表格的形式呈现。之后 我已经完成了选择,并将其复制到剪贴板中。
我意识到,我可能需要为我捕获的数据编写逻辑,然后进行格式化。实现这个目标的最佳方法是什么?
下面是我目前写的代码。
这是我的代码
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
你可以直接将你的数据读到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设置为第一生,并选择了列表中的第二个元素,也就是你要找的表。