【问题标题】:Getting HTML elements using requests and BeautifulSoup使用请求和 BeautifulSoup 获取 HTML 元素
【发布时间】:2017-03-05 03:21:27
【问题描述】:

晚上好,伙计们。 我正在尝试使用请求和 BeautifulSoup 获取一些 html 元素。使用请求的内容作为解析器的参数,我想在股票价格历史网站上获得最终报价。此信息存储在此链接 (https://iqoption.com/pt/historical-financial-quotes?active_id=1&tz_offset=-180&date=2017-3-4-0-0) 中的“quotes-table-result__val”类中。但是,该脚本返回一个 None 对象。你知道我做错了什么吗?

代码:

from bs4 import BeautifulSoup
import requests

r = requests.get('https://iqoption.com/pt/historical-financial-quotes?active_id=1&tz_offset=-180&date=2017-3-4-0-0')
soup = BeautifulSoup(r.content, 'html5lib')
final_quotation = soup.find(class_='quotes-table-result__val')

【问题讨论】:

    标签: python html beautifulsoup python-requests


    【解决方案1】:

    请注意,当您搜索给定历史记录时,此网站会请求 GET 方法,并在该请求完成后加载表中的数据。

    您必须模拟请求并解析结果。就我所见,请求方法将返回一个 json 对象。

    请注意请求中的 date 参数。你会在那里有你想要的日期。

    详细了解requestsjson parsing here

    这是一个示例代码:

    import requests
    
    r = requests.get('https://eu.iqoption.com/api/quote/history/v1/expirations?active_id=1&tz_offset=-180&date=2017-3-4-0-0')
    JSON_OBJECT = r.json()
    
    for datetime_object in JSON_OBJECT["result"]["expirations"]:
        Bid = datetime_object["bid"]
        Ask = datetime_object["ask"]
        Value = datetime_object["value"]
    
        print ("DateTime: %s - Bid (%s) - Ask (%s) - Value (%s)" % (datetime_object["datetime"], Bid, Ask, Value))
    

    哪些输出:

    日期时间:2017-03-04 00:00:00 - 买价 (1.06241) - 卖价 (1.06224) - 价值 (1.062325)

    日期时间:2017-03-04 00:00:01 - 买价 (1.06241) - 卖价 (1.06224) - 价值 (1.062325)

    日期时间:2017-03-04 00:00:02 - 买价 (1.06241) - 卖价 (1.06224) - 价值 (1.062325)

    ...

    【讨论】:

    • BeautifulSoup 有没有办法做到这一点?
    • Beautifulsoup 没有准备好 ro 句柄 json。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多