【问题标题】:Webscrape incomplete after turning to DataFrame转向 DataFrame 后 Webscrape 不完整
【发布时间】:2020-06-14 00:41:43
【问题描述】:

我正在从该网站https://www.iban.com/country-codes 的表格中进行网络抓取,但将其放入 DataFrame 时,抓取的信息不完整。

# Webscrape list of official countries with country codes
url = 'https://www.iban.com/country-codes'
response = requests.get(url) 
page = response.content
scraping = BeautifulSoup(page, "lxml") 
scraping
element = scraping.find("table", attrs={"class" : "table table-bordered downloads tablesorter"})
df = pd.read_html(str(element))
countrycodes = df[0]

因此,例如,菲律宾 (the)、大不列颠及北爱尔兰联合王国 (the)、瑞士等国家/地区不属于数据框。

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    数据在DataFrame中。当您将countrycodes 打印到屏幕上时,pandas 将缩短数据框并放置... 而不是行。

    为了演示,此代码将加载表格并将其保存到 CSV:

    import pandas as pd
    
    df = pd.read_html('https://www.iban.com/country-codes')[0]
    df.to_csv('data.csv')
    

    生成此 CSV(突出显示的是 "Philippines (the)"):

    【讨论】:

    • 谢谢!我已经发现了这个问题并修复了它。它在不同的数据框中,所以当我将它们合并在一起时,一些国家被删除了:)
    猜你喜欢
    • 1970-01-01
    • 2017-01-16
    • 2011-08-29
    • 2020-04-30
    • 2021-10-13
    • 2015-03-16
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多