【问题标题】:How to download multiple pages of data from this web service?如何从此网络服务下载多页数据?
【发布时间】:2015-11-09 12:35:03
【问题描述】:

我正在尝试下载 2015 年加拿大联邦选举的所有候选人的数据。有一个名为 opennorth 的服务有一个 API,允许您通过向该 url 发送请求来执行此操作:

https://represent.opennorth.ca/candidates/?limit=1000

1000 个候选人是您在单个请求中被允许的限制,但肯定还有更多。我想知道如何获得下一页结果。来自他们自己的文档:

要下载所有代表,请发送请求至 https://represent.opennorth.ca/representatives/?limit=1000 并关注 元字段下的下一个链接,直到您到达末尾。我们主办 GitHub 上的 shapefile 和邮政编码索引。

这是“代表”数据,但我认为“候选人”也是如此。我不明白他们所说的“跟随元字段下的下一个链接直到你到达终点”是什么意思。有人能告诉我这个吗?

到目前为止,这是我的脚本:

import urllib

with urllib.request.urlopen(r"https://represent.opennorth.ca/candidates/house-of-commons/?limit=1000") as url:
    with open(r"F:\electoral_map\candidates_python\candidates.js", "wb+") as f:
        f.write(url.read())
print("all done")

【问题讨论】:

    标签: python web-scraping urllib


    【解决方案1】:

    在返回的 JSON 对象中,有一个名为 meta 的对象。

    ..."meta": {"next": "/representatives/?limit=1000&offset=1000",
                "total_count": 2140,
                "previous": null,
                "limit": 1000,
                "offset": 0}}
    

    您需要的链接位于["meta"]["next"]

    或者,您可以通过添加 offset URL 参数来构建该链接。

    【讨论】:

    • 太棒了!谢谢!
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多