将CME价格数据拉入python3.6.8

2024-09-25 16:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python比较陌生,所以如果这是布什联盟的问题,我会道歉。

我试图从本网站检索WTI期货价格: https://www.cmegroup.com/trading/energy/crude-oil/west-texas-intermediate-wti-crude-oil-calendar-swap-futures_quotes_globex.html

我应该使用哪些库?当输出从网站上拉出来时,我需要如何调整?

目前使用Python3.6.8操作熊猫、numpy、requests、URLLB3、Beautiulsoup和json库。我不确定这些库是否正确,以及它们是否是我应该使用的函数。

以下是代码的基本版本:

wtiFutC = 'https://www.cmegroup.com/trading/energy/crude-oil/west-texas-intermediate-wti-crude-oil-calendar-swap-futures_quotes_globex.html'
http = urllib3.PoolManager()
response2 = http.request('GET', wtiFutC)
print(type(response2.data)) #check the type of the data produced - bytes
print(response2.data) #prints out the data

soup2 = BeautifulSoup(response2.data.decode('utf-8'), features='html.parser')
print(type(soup2)) #check the type of the data produced - 'bs4.BeautifulSoup'
print(soup2) #prints out the BeautifulSoup version of the data

我想找一个方法来看看WTI未来的最后一个价格。相反,我看到的是这样的东西:

^{pr2}$

如有任何帮助或指示,将不胜感激。非常感谢!:)


Tags: ofthehttpsdata网站htmlwwwtype
3条回答

使用页面所做的端点并从json解析出感兴趣的列(和日期)

import requests

r = requests.get('https://www.cmegroup.com/CmeWS/mvc/Quotes/Future/4707/G?quoteCodes=null&_=1560171518204').json()
last_quotes = [(item['expirationDate'], item['last']) for item in r['quotes']]

该网页中的数据是javascript生成的,因此很难用requests之类的包提取数据。 如果这不是什么大问题,我建议您寻找一个使用最少或不使用javascript的不同数据源。然后使用requests获取网页源并从中提取数据。在

您可以使用BeautifulSoupre(在某些情况下甚至是pandas)之类的库提取数据,并将它们馈送到numpy或{}之类的库中,如果您想对数据进行分析和计算的话。在

否则,我建议您看看Selenium以获得javascript支持。在

使用Requests-HTML。如果你已经熟悉了请求,这是一个很好的资源。在

相关问题 更多 >