【问题标题】:downloading data using python from a website that uses javascript to display information使用python从使用javascript显示信息的网站下载数据
【发布时间】:2015-11-15 00:36:57
【问题描述】:

我通常使用以下模板脚本从网站下载数据:

import urllib.request as web
from bs4 import BeautifulSoup
...
url_to_visit ='http://www.website-link-to-download-data'
source_code =  web.urlopen(url_to_visit).read()
source_code = ''.join(map(chr, source_code)
source_code = source_code.split('\n')
## then further process the lines returned in `source_code` as needed

但有时我会遇到非常困难的网站。

考虑网站:https://www.spice-indices.com/idp2/Main#home。假设从第一个表Intraday Alerts - United States,我想通过Python脚本下载点击SP TMI标签时显示的信息。

我查看了上面splitSource 的输出,但我不知道如何提取我想要的信息。它似乎正在使用 Javascript 后端来显示信息。有人可以给我任何指示或建议吗?

我正在使用 Python 3.x。

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup


    【解决方案1】:

    当您激活“SP TMI”选项卡时,会向“intraday-announcements.json”端点发送 POST 请求 - 在您的代码中模拟并解析 JSON 响应。

    使用requests的示例工作代码:

    import requests
    
    with requests.Session() as session:
        session.get("https://www.spice-indices.com/idp2/Main#home")
    
        response = session.post("https://www.spice-indices.com/idp2/intraday/effectivedate/11-14-2015/intraday-announcements.json", data={
            "start": "0",
            "limit": "10",
            "indexKey": "SPUSA-TMI-USDUF--P-US----"
        })
    
        data = response.json()["widget_data"]
        for item in data:
            print(item["EVENT_NAME"])
    

    打印:

    Dividend
    Weekly Share Change
    Special Dividend
    Merger/Acquisition
    Merger/Acquisition
    Drop
    Merger/Acquisition
    Merger/Acquisition
    Drop
    Identifier Changes
    

    请注意,生效日期实际上是在 URL 中,请参阅 11-14-2015 部分。

    【讨论】:

    • 这个页面的一个快速问题:http://www.ftse.com/products/index-notices/home/getnotices/?id=GEISAC&title=,我如何找到json 或等效的request
    • @uday 很高兴为您提供帮助。只需使用浏览器开发人员工具并检查发出的请求。快速浏览后,我认为有一个对 http://www.ftse.com/products/index-notices/Backend/GetNotices 端点的 GET 请求,其中包含页面上的通知列表。如果您在获取所需数据时遇到困难,请考虑提出一个新的单独问题,以便更多人可以提供帮助。谢谢!
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多