从维基百科表中抓取数据

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

我只是想把维基百科表中的数据写入熊猫数据帧。

我需要重现三列:“邮政编码,自治市镇,社区”。

import requests
website_url = requests.get('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M').text
from bs4 import BeautifulSoup
soup = BeautifulSoup(website_url,'xml')
print(soup.prettify())

My_table = soup.find('table',{'class':'wikitable sortable'})
My_table

links = My_table.findAll('a')
links

Neighbourhood = []
for link in links:
    Neighbourhood.append(link.get('title'))

print (Neighbourhood)

import pandas as pd
df = pd.DataFrame([])
df['PostalCode', 'Borough', 'Neighbourhood'] = pd.Series(Neighbourhood)

df

它只返回自治市...

谢谢

python pandas beautifulsoup wikipedia
3个回答
0
投票

如果您只希望脚本从页面中拉出一个表,您可能会过度思考问题。一个导入,一行,无循环:

import pandas as pd
url='https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M'

df=pd.read_html(url, header=0)[0]

df.head()

    Postcode    Borough         Neighbourhood
0   M1A         Not assigned    Not assigned
1   M2A         Not assigned    Not assigned
2   M3A         North York      Parkwoods
3   M4A         North York      Victoria Village
4   M5A         Downtown Toronto    Harbourfront

1
投票

您需要迭代表中的每一行并逐行存储数据,而不只是在一个巨大的列表中。尝试这样的事情:

import pandas
import requests
from bs4 import BeautifulSoup
website_text = requests.get('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M').text
soup = BeautifulSoup(website_text,'xml')

table = soup.find('table',{'class':'wikitable sortable'})
table_rows = table.find_all('tr')

data = []
for row in table_rows:
    data.append([t.text.strip() for t in row.find_all('td')])

df = pandas.DataFrame(data, columns=['PostalCode', 'Borough', 'Neighbourhood'])
df = df[~df['PostalCode'].isnull()]  # to filter out bad rows

然后

>>> df.head()

  PostalCode           Borough     Neighbourhood
1        M1A      Not assigned      Not assigned
2        M2A      Not assigned      Not assigned
3        M3A        North York         Parkwoods
4        M4A        North York  Victoria Village
5        M5A  Downtown Toronto      Harbourfront

0
投票

Basedig提供了一个平台,可以直接将维基百科表格下载为Excel,CSV或JSON文件。以下是维基百科来源的链接:https://www.basedig.com/wikipedia/

如果您没有在Basedig上找到您要查找的数据集,请将链接发送给您的文章,他们会为您解析。希望这可以帮助

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