【问题标题】:Get countries and capitals from website Python从网站 Python 获取国家和首都
【发布时间】:2022-11-02 13:42:17
【问题描述】:

我需要从https://geographyfieldwork.com/WorldCapitalCities.htm 获取国家和首都,并创建包含 2 列的 DataFrame:国家、首都 使用模块“请求”

我尝试使用're'模块,但它没有用(

【问题讨论】:

  • 您正在尝试做的事情称为网络抓取(从 html 中提取数据),并且可以使用诸如 beautifulsoup 之类的库来实现。但是,对于您的用例,您可以使用 requests 模块从 Rest API 获取数据。这是您的用例的 API:countriesnow.space/api/v0.1/countries/capital
  • 该问题需要足够的代码来实现最小的可重现示例:stackoverflow.com/help/minimal-reproducible-example
  • 此外,您需要修复问题中的链接。单击它只会将我带到主 stackoverflow 页面。

标签: python parsing python-requests python-re


【解决方案1】:
使用 pd.read_html() 返回一个列表。您想要的表格在索引 2.import pandas as pd url = "https://geographyfieldwork.com/WorldCapitalCities.htm" df = pd.read_html(url)[2] print(df) 中。

【讨论】:

    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多