gpt4 book ai didi

python - 网络抓取(获取表格时出现问题)

转载 作者:行者123 更新时间:2023-12-01 03:35:06 25 4
gpt4 key购买 nike

我正在尝试从表中获取信息。但是由于某种原因我不能。我不知道代码中到底缺少什么。在获取另一个表时,相同的代码可以工作(只需少量更改)。

这是我的代码。

import urllib.request
from bs4 import BeautifulSoup
import pandas as pd


htmlfile = urllib.request.urlopen("https://en.wikipedia.org/wiki/Demographics_of_Finland")
htmltext = htmlfile.read()
soup = BeautifulSoup(htmltext)

all_tables=soup.find_all('table')

right_table=soup.find('table', class_='wikitable')
right_table



#Generate lists
A=[]
B=[]
C=[]
D=[]
E=[]
F=[]
G=[]
H=[]
I=[]
for row in right_table.findAll("tr"):
cells = row.findAll('td')
states=row.findAll('th') #To store second column data
if len(cells)==8: #Only extract table body not heading
A.append(cells[0].find(text=True))
B.append(states[0].find(text=True))
C.append(cells[1].find(text=True))
D.append(cells[2].find(text=True))
E.append(cells[3].find(text=True))
F.append(cells[4].find(text=True))
G.append(cells[5].find(text=True))
H.append(cells[6].find(text=True))
I.append(cells[7].find(text=True))



#import pandas to convert list to data frame

df=pd.DataFrame(A,columns=['Number'])
df['Average´_population_(x 1000)']=B
df['Live_births']=C
df['Deaths']=D
df['Natural_change']=E
df['Crude_birth_rate_(per 1000)']=F
df['>Crude_death_rate_(per_1000)']=G
df['>Natural_change_(per 1000)']=H
df['>Total_fertility_rate']=I
df

最佳答案

我认为你需要read_html - 函数从 url 返回 html 表的 Dataframeslist。您需要第三个 df,即 [2]。另外,表格的第一行是标题 - 添加参数 header=0,第一列可以通过参数 index_col=0 解析为 index:

import pandas as pd

df = pd.read_html('https://en.wikipedia.org/wiki/Demographics_of_Finland',
header=0,
index_col=0)[2]
print (df)
Average population (x 1000) Live births Deaths Natural change \
1900 2 646 86 339 57 915 28 424
1901 2 667 88 637 56 225 32 412
1902 2 686 87 082 50 999 36 083
1903 2 706 85 120 49 992 35 128
1904 2 735 90 253 50 227 40 026
1905 2 762 87 841 52 773 35 068
1906 2 788 91 401 50 857 40 544
1907 2 821 92 457 53 028 39 429
1908 2 861 92 146 55 305 36 841
1909 2 899 95 005 50 577 44 428
1910 2 929 92 984 51 007 41 977
1911 2 962 91 238 51 648 39 590
1912 2 998 92 275 51 645 40 630
1913 3 026 87 250 51 876 35 374
...
...

关于python - 网络抓取(获取表格时出现问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com