我正在尝试使用python bs4在维基百科中抓取数据表。但是我一直坚持这个问题。获取数据值时,我的代码不会获取第一列或索引零。我觉得索引有问题,但我无法弄清楚。请帮忙。请参阅response_obj = requests.get('https://en.wikipedia.org/wiki/Metro_Manila').textsoup = BeautifulSoup(response_obj,'lxml')Neighborhoods_MM_Table = soup.find('table', {'class':'wikitable sortable'})rows = Neighborhoods_MM_Table.select("tbody > tr")[3:8]cities = []for row in rows: city = {} tds = row.select('td') city["City or Municipal"] = tds[0].text.strip() city["%_Population"] = tds[1].text.strip() city["Population"] = float(tds[2].text.strip().replace(",","")) city["area_sqkm"] = float(tds[3].text.strip().replace(",","")) city["area_sqm"] = float(tds[4].text.strip().replace(",","")) city["density_sqm"] = float(tds[5].text.strip().replace(",","")) city["density_sqkm"] = float(tds[6].text.strip().replace(",","")) cities.append(city)print(cities)df=pd.DataFrame(cities)df.head()
1 回答
万千封印
TA贡献1891条经验 获得超3个赞
import requests
from bs4 import BeautifulSoup
import pandas as pd
def main(url):
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
target = [item.get_text(strip=True) for item in soup.findAll(
"td", style="text-align:right") if "%" in item.text] + [""]
df = pd.read_html(r.content, header=0)[5]
df = df.iloc[1: -1]
df['Population (2015)[3]'] = target
print(df)
df.to_csv("data.csv", index=False)
main("https://en.wikipedia.org/wiki/Metro_Manila")
输出:在线查看
添加回答
举报
0/150
提交
取消