Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 站点数据未作为浏览器填充,尽管使用html请求呈现_Python_Web Scraping_Python Requests Html - Fatal编程技术网

Python 站点数据未作为浏览器填充,尽管使用html请求呈现

Python 站点数据未作为浏览器填充,尽管使用html请求呈现,python,web-scraping,python-requests-html,Python,Web Scraping,Python Requests Html,我正在各种网站上试验html请求, 我在提取这个特定站点上的股票价格时遇到困难: 我使用html请求,并使用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; x

我正在各种网站上试验
html请求
, 我在提取这个特定站点上的股票价格时遇到困难:

我使用html请求,并使用
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%
但是,, 或者我只是想找回一些符号:
-
%
但没有实际价格

有什么建议吗?
谢谢。

数据由JSON API生成,然后通过JavaScript动态插入网站,因此
python请求
无法看到它。您可以通过执行
curl来验证它https://www.morningstar.com/stocks/xnys/baba/quote
并尝试在其上查找
1.65%
——它不在那里,只是因为它不在HTML源代码中

我建议改为使用,并按如下方式解析数据:

elements = driver.find_element(By.ID, "div")
for element in elements:
    print element.text
    print element.get_attribute('message-box-price.message-partial.fill.up')

我在这页上找不到股票的价格。嗨,如果你向下滚动大约1/4,它就在图表的正上方。我又查了一遍。引号位于绿色框中。HTML中不存在所需信息它来自JSON API响应
https://api-global.morningstar.com
。查看chrome中的网络选项卡了解更多详细信息谢谢。然而,我使用的是
请求html
,它通过
pypuppeter
呈现
javascript
,这与selenium非常相似,但更健壮。很抱歉,我错过了这一点。我已经对从python请求到python请求html的标记进行了编辑。