簡體   English   中英

Python Beautiful Soup 從 2 個 URL 獲取數據

[英]Python Beautiful Soup get data from 2 URL's

美好的一天,我是上 python 節課的學生。 我們現在正在學習 Beautiful Soup,我在從 2 個表中提取數據時遇到了問題,您將在下面的代碼中看到:

import pandas as pd
import requests

list_of_urls = ['https://tradingeconomics.com/albania/gdp-growth-annual',
                'https://trdingeconomics.com/south-africa/gdp-growth-annual']

final_df = pd.DataFrame()

for i in lists_of_urls:
    table = pd.read_html(i, match='Related')
    for row in table:
        if row.loc['Related'] == 'GDP Annual Growth Rate':
            final_df.append(row)
        else:
            pass

您既不需要requests也不需要bs4 pd.read_html完成這項工作。

list_of_urls = ['https://tradingeconomics.com/albania/gdp-growth-annual',
                'https://tradingeconomics.com/south-africa/gdp-growth-annual']

data = {}
for i in list_of_urls:
    country = i.split('/')[3]
    df = pd.read_html(i, match='Related')[0]
    data[country] = df.loc[df['Related'] == 'GDP Annual Growth Rate']

df = pd.concat(data)

Output:

>>> df
                               Related  Last  Previous     Unit Reference
albania      1  GDP Annual Growth Rate  6.99     18.38  percent  Sep 2021
south-africa 1  GDP Annual Growth Rate  1.70      2.90  percent  Dec 2021

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM