【发布时间】:2020-08-05 02:12:03
【问题描述】:
我正在各个网站上尝试html-requests,
我无法在这个特定网站上提取股票价格:
https://www.morningstar.com/stocks/xnys/BABA/quote
我正在使用 html-requests,并使用 html.render 来呈现 javascript。
尽管如此,数据似乎并没有像在浏览器中那样被填充。
from requests_html import HTMLSession
import requests_html
from bs4 import BeautifulSoup as bs
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
requests_html.DEFAULT_USER_AGENT = user_agent
def get_request(ticker):
session = HTMLSession()
print(url)
res = session.get(url)
try:
res.raise_for_status()
except ValueError as e:
raise('Dead link')
return res
def mstar():
url = 'https://www.morningstar.com/stocks/xnys/BABA/quote'
res = get_requesturl)
res.html.render()
price = res.html.find('div#message-box-price.message-partial.fill.up')[0].text
print(price)
price = res.html.find('div.message-partial.fill.up')[0].text
print(price)
change = res.html.find('div#message-box-percentage')[0].text
print(change)
预期结果是这个数据:
262.20
4.26 | 1.65%
然而,
要么我只是找回符号:
- 或 % 但没有实际价格。
有什么建议吗? 谢谢。
【问题讨论】:
-
我在这个页面上没有找到股票的价格。
-
您好,如果您向下滚动页面约 1/4,它就在图表上方。我刚刚又检查了一遍。引号在绿色框中。
-
HTML 中不存在所需信息,它来自 JSON API 响应
https://api-global.morningstar.com。检查 chrome 中的网络选项卡以获取更多详细信息
标签: python web-scraping python-requests-html