dataframe - 需要在 Jupyter 中提取 html 链接
问题描述
我浏览了尽可能多的存储库,发现编写代码从维基百科页面中提取元素以及每个城市的元素都缺失了。
import pandas as pd
url='https://en.wikipedia.org/wiki/List_of_cities_in_New_York'
df=pd.read_html(url, header=0)[0]
df.head()
import pandas
import requests
from bs4 import BeautifulSoup
website_text = requests.get('https://en.wikipedia.org/wiki/List_of_cities_in_New_York').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=['City', 'PostalCode',
'Population','IncorpDate','FIPS_Sub','FIPS_Place'])
df = df[~df['PostalCode'].isnull()] # to filter out bad rows
df.head()
df.to_csv('ny_cities22.csv', encoding='utf-8')
我知道这可能是我遗漏的东西,但我无法弄清楚代码。
谢谢。
解决方案
推荐阅读
- javascript - 如何在 JSON 中获取玩家物品数组中的所有非拥有物品
- ios - 本机 UITextField 安全文本输入强制使用英语(美国)键盘
- ubuntu - 带有 conjure-up 和 MAAS 的 Openstack
- javascript - 在 Jison 中使用自定义函数
- kubernetes - 度量服务器的 kubernetes hpa 问题
- python - 使用 pyinstaller 生成的 .exe 在其他 PC 上崩溃 - 如何创建真正没有依赖关系的 .exe?
- r - 折线图问题 - 情节看起来“有趣”(ggplot2)
- wordpress - Wordpress 如何按日期选择器搜索帖子?
- java - RecyclerView Android 动态高度
- asp.net-mvc - .NET + Angular - 当从火狐发出请求时,IIS 服务器在 chrome 中刷新 localhost 选项卡