Я хотел бы получить список избранных символов с соответствующими ценами для этого символа на веб-сайте CME ниже. Я могу получить список символов, но я не смог понять, как вывести цены в каждом ряду.
Возникают проблемы при использовании «Проверять» в браузере теги для запроса, отличные от «span». Мысли, чтобы решить эту проблему?
Код:
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)