猿问

如何使用python bs4获取维基百科表格中的第一列值?

我正在尝试使用python bs4在维基百科中抓取数据表。但是我一直坚持这个问题。获取数据值时,我的代码不会获取第一列或索引零。我觉得索引有问题,但我无法弄清楚。请帮忙。请参阅

response_obj = requests.get('https://en.wikipedia.org/wiki/Metro_Manila').text

soup = 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() 


慕侠2389804
浏览 152回答 1
1回答

万千封印

import requestsfrom bs4 import BeautifulSoupimport pandas as pddef 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")输出:在线查看
随时随地看视频慕课网APP

相关分类

Python
我要回答