将 CME 价格数据拉入 Python 3.6.8

Pull CME price data into Python 3.6.8

我对 Python 比较陌生,所以如果这是一个 'bush league' 问题,我深表歉意。

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

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

目前在 Python 3.6.8 中使用 pandas、numpy、requests、urllib3、BeautifulSoup 和 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 期货的 'Last' 价格。相反,我看到的是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!--[if (gt IE 9) |!(IE)]><!-->
<html class="cmePineapple no-js" lang="en" xml:lang="en" 
xmlns="http://www.w3.org/1999/xhtml">
<!--<![endif]-->

如有任何帮助或指导,我们将不胜感激。太感谢了! :)

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

您使用 BeautifulSoupre(在某些情况下甚至 pandas)等库提取数据,并将它们提供给 numpypandas 如果你想对数据进行分析和计算。

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

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

使用页面执行的端点并从 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']]