【发布时间】:2020-09-09 19:31:40
【问题描述】:
我正在尝试从网络创建一个Dataframe,并通过不同部分抓取相同的网页,但是在尝试表示列时,我收到此错误:
"Length mismatch: Expected axis has 5 elements, new values have 8 elements"
url='https://money.cnn.com/magazines/fortune/fortune500_archive/full/1955/1.html'
webcontent=urlopen(url)
html_page=webcontent.read()
soup=BeautifulSoup(html_page, "lxml")
table=soup.select("table")[0]
rows= table.select('tr')
table_data=[]
for row in rows:
td_tag=row.select('td')
row_values=[value.string for value in td_tag]
table_data.append(row_values)
data=pd.DataFrame(table_data[1:])
cols=[header.string for header in table.select('th')]
data.columns= cols
data.head()
非常感谢您的帮助!
【问题讨论】:
标签: python dataframe web-scraping beautifulsoup urllib