使用 Python 和 BeautifulSoup 提取 CME 数据

麦克

我想从下面的 CME 网站中提取一个选择的符号列表以及该符号的相关价格。我能够得到一个符号列表,但我无法弄清楚如何拉动每一行的价格。

在浏览器上使用“检查”时遇到问题,要查询除“跨度”以外的标签。让我解决这个问题的想法?

代码:

import urllib
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup

def simple_get(url):
    """
    Attempts to get the content at `url` by making an HTTP GET request.
    If the content-type of response is some kind of HTML/XML, return the
    text content, otherwise return None.
    """
    try:
        with closing(get(url, stream=True)) as resp:
            if is_good_response(resp):
                return resp.content
            else:
                return None

    except RequestException as e:
        log_error('Error during requests to {0} : {1}'.format(url, str(e)))
        return None

def is_good_response(resp):
    """
    Returns True if the response seems to be HTML, False otherwise.
    """
    content_type = resp.headers['Content-Type'].lower()
    return (resp.status_code == 200 
            and content_type is not None 
            and content_type.find('html') > -1)

def log_error(e):
    print(e)

raw_html = simple_get('https://www.cmegroup.com/trading/price-limits.html#equityIndex')
html = BeautifulSoup(raw_html, 'html.parser', store_line_numbers=True)
seq = ['ESM0', 'NQM0', 'RTYM0', 'YMM0'] 

for quote in html.find_all('span'):
    symbolcme = quote.get_text(strip=True)
    #print("Check Symbol: ", symbolcme)
    for text in seq:
        if text in symbolcme: 
            print(quote.sourceline, ' Symbol:', symbolcme)

结果:

2014  Symbol: E-mini S&P 500 Futures (ESM0)
2047  Symbol: E-mini Nasdaq-100 Futures (NQM0)
2065  Symbol: E-mini Dow ($5) Futures (YMM0)
2392  Symbol: E-mini  Russell 2000 Index Futures (RTYM0)
2500  Symbol: Micro E-mini Dow Jones Industrial Average Index Futures (MYMM0)
2515  Symbol: Micro E-mini Nasdaq-100 Index Futures (MNQM0)
2551  Symbol: Micro E-mini S&P 500 Index Futures (MESM0)
Xevion

BeautifulSouptag.parenttag.next_siblings在这里可以很好地协同工作,假设您希望价格与打印的报价在同一行。

for quote in html.find_all('span'):
    symbolcme = quote.get_text(strip=True)
    for text in seq:
        if text in symbolcme:
            print(quote.sourceline, ' Symbol:', symbolcme)
            prices = [sibling.get_text() for sibling in quote.parent.next_siblings]
            print(prices)

输出:

2015  Symbol: E-mini S&P 500 Futures (ESM0)
['240000', '252000 / 228000', '223150', '208700', '191850']
2048  Symbol: E-mini Nasdaq-100 Futures (NQM0)
['726475', '762900 / 690050', '675475', '631725', '580725']
2066  Symbol: E-mini Dow ($5) Futures (YMM0)
['19951', '20955 / 18947', '18545', '17340', '15934']
2393  Symbol: E-mini  Russell 2000 Index Futures (RTYM0)
['104070', '109360 / 98780', '96660', '90310', '82900']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用<script>和var从BeautifulSoup中提取数据

如何解析呢?尝试使用BeautifulSoup和Python从非HTML网页提取数据

使用BeautifulSoup从tbody提取数据

尝试使用Python和BeautifulSoup提取准确值

使用BeautifulSoup提取网站数据

Python BeautifulSoup从标题中提取数据

将CME价格数据提取到Python 3.6.8中

使用python selenium / Beautifulsoup从多个页面提取数据

使用BeautifulSoup和Requests提取数据

如何使用API和Python提取数据?

使用Beautifulsoup从网站提取数据

Python-使用Beautifulsoup从网页提取数据

使用BeautifulSoup和for循环提取数据

Xpath和CssSelector不使用Selenium和Python提取数据

从<script> BeautifulSoup Python提取数据

使用BeautifulSoup提取数据

无法使用beautifulsoup提取表数据

使用beautifulsoup和request提取json数据

我正在尝试使用python3和beautifulsoup4为我的学校项目提取数据

使用 Python 和 BeautifulSoup 提取数字(多个跨度和类)

使用beautifulsoup和python从json中提取数据

如何在Python和beautifulsoup中从CDATA中提取数据?

使用 Python 和 BeautifulSoup 从嵌入的推文中提取文本

使用 BeautifulSoup 解析和提取数据到 Pandas

使用 python 提取和绘制 XML 数据

在 Python 中使用 BeautifulSoup 从 html 中提取数据

使用 BeautifulSoup 和 Python 提取 iframe

我不能使用不同的函数从beautifulsoup python中提取数据和过滤掉

如何使用 Python 和 BeautifulSoup 从 cricinfo 中提取玩家姓名