第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

慕俠2389804 2022-10-05 10:09:58
我正在嘗試使用python bs4在維基百科中抓取數(shù)據(jù)表。但是我一直堅持這個問題。獲取數(shù)據(jù)值時,我的代碼不會獲取第一列或索引零。我覺得索引有問題,但我無法弄清楚。請幫忙。請參閱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條經(jīng)驗 獲得超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")

輸出:在線查看

查看完整回答
反對 回復 2022-10-05
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號