【问题标题】:How do I scrape the data from the Google Docs table on this web page?如何从该网页上的 Google Docs 表中抓取数据?
【发布时间】:2016-12-19 03:08:00
【问题描述】:

我正在尝试使用 Python 从该网页上的表格中抓取数据。

http://www.dividendyieldhunter.com/exchanged-traded-debt-issues-sorted-alphabetically/

我尝试使用 requests 和 bs4。我得到了原始 HTML,但看起来数据是隐藏的。我应该尝试什么?

【问题讨论】:

    标签: python web beautifulsoup screen-scraping


    【解决方案1】:

    该特定页面正在此代码中从 iFrame 中的 URL 加载数据:

    <iframe id="pageswitcher-content" frameborder="0" marginheight="0" marginwidth="0" src="https://docs.google.com/spreadsheets/d/1_HY2XEBKcyi4STki-uUbOfr-su8CZOfpi-jM1Racwyw/pubhtml/sheet?headers=false&amp;gid=0" style="display: block; width: 100%; height: 100%;"></iframe>
    

    您需要进一步从 src 属性中的 URL 请求 HTML:

    https://docs.google.com/spreadsheets/d/1_HY2XEBKcyi4STki-uUbOfr-su8CZOfpi-jM1Racwyw/pubhtml/sheet?headers=false&amp;gid=0
    

    然后你可以用 class="waffle" 刮桌子。

    注意:请注意来自原始 URL 的 URL 查询参数,如下例所示。

    例如,靠近末尾的 &amp;amp; 必须转换为单个 & 字符,请求模块才能找到正确的 URL,例如

    import requests
    res=requests.get("https://docs.google.com/spreadsheets/d/1_HY2XEBKcyi4STki-uUbOfr-su8CZOfpi-jM1Racwyw/pubhtml/sheet?headers=false&gid=0")
    print(res.text)
    

    【讨论】:

    • 所以如果我使用带有 iframe ip 地址的请求然后打印结果,我看不到表格或任何数据。
    • @ Turnkey 因此,如果我使用带有 iframe ip 地址的请求然后打印结果,我看不到表格或任何数据。导入请求 res=request.get(''docs.google.com/spreadsheets/d/…' print(res.text)
    • 我认为尝试自己获取它时会发生阻塞。让我再做一次测试,然后回来陪你。
    • URL 有问题,&需要将请求代码转换为实际的 &。我会相应地更新答案。
    • 成功了!非常感谢。不过有几个问题。你是怎么想出来的?怎么会知道 &部分 url 字符串不合适?如果 iframe 中的字符串不正确,表格如何实际更新?任何信息,将不胜感激。只是想学习。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2018-02-24
    • 2013-05-21
    • 2021-01-13
    相关资源
    最近更新 更多