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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從維基百科獲取和解析數(shù)據(jù)

從維基百科獲取和解析數(shù)據(jù)

長(zhǎng)風(fēng)秋雁 2023-08-08 17:31:29
我正在嘗試從維基百科獲取并解析一些數(shù)據(jù)以進(jìn)行分析。我正在嘗試提取數(shù)據(jù)來(lái)分析列中因素與幸福分?jǐn)?shù)本身之間的相關(guān)性。但該文件未按預(yù)期工作。事實(shí)上,最終的文件是空的。我嘗試調(diào)試,但沒(méi)有成功:import requestsimport bs4from bs4 import BeautifulSoupimport csvfrom csv import DictWriterdef get_page(url):    html_file = requests.get(url)    return html_filedef parse_html(html_file):    parsed_html = bs4.BeautifulSoup(html_file.text, 'html.parser')        t = parsed_html.find_all('table')[1]        tr = t.find_all('tr')[0]    headers = []    for header in tr:        for z in header:            if isinstance(z, bs4.element.NavigableString):                header_name = z.strip()                headers.append(header_name)        headers_original = headers    countries = []    prev_rank = ""    for ln, row in enumerate(t.find_all('tr')[2:]):        country = {}        i = 0        col_values = row.find_all('td')                while i < len(headers):            col = col_values[i]            value = col.text.strip()                            if headers[i] in ('Country or region',):                value = str(value)            else:                value = float(value)            country[headers[i]] = value            i += 1        countries.append(country)    return headers, countriesdef write_csv(filename, data, fieldnames):    with open(filename, 'w') as csvfile:        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)        writer.writeheader()        writer.writerows(data)def main():    source = "https://en.wikipedia.org/wiki/World_Happiness_Report"    print(f'Fetching URL {source}...')    page = get_page(source)    print(f'Parsing {source}...')    headers, countries = parse_html(page)    print(f'Writing to a CSV file 1...')    write_csv('worldhappiness.csv', countries, headers)    print(f'Parsing table 3 (simpler table) {source} ...')    headers, countries = parse_html(page)
查看完整描述

1 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用 pandas 來(lái)實(shí)現(xiàn)此目的要簡(jiǎn)單得多:

import pandas as pd
tables = pd.read_html('https://en.wikipedia.org/wiki/World_Happiness_Report')
tables[4]

輸出是您的目標(biāo)表。


查看完整回答
反對(duì) 回復(fù) 2023-08-08
  • 1 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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