使用Selenium和Python抓取Morningstar网站。Selenium没有下载完整的网页

胡吉

这是我的代码:

from selenium import webdriver
import pandas as pd
from lxml import etree

url = 'https://www.morningstar.com/stocks/xbsp/UGPA3/quote'
browser = webdriver.Chrome()
browser.get(url)
htmlpage = browser.page_source

doc = etree.HTML(htmlpage)
cap = doc.xpath(
    '/html/body/div[1]/div/div/div[3]/main/div[2]/div/div/div[1]/sal-components/section/div/div/div[1]/div/div[2]/div/div/div/div[2]/ul/li[7]/div/div[2]/text()')

print(cap)

我正在尝试从网页上抓取市值。

将htmlpage变量写入文件后,我发现问题是它没有下载整个页面。它下载2228 KB,而我的浏览器下载2664 KB .html文件以及一个不需要的文件夹。如果我使用浏览器手动保存页面并将其内容用作etree.HTML()的输入,则它可以工作,但我想实现自动化。

马拉里·卡加萨拉(Manali Kagathara)

尝试这个

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    import time

    CHROME_DRIVER_PATH = "/usr/local/bin/chromedriver"

    url = 'https://www.morningstar.com/stocks/xbsp/UGPA3/quote'
    browser = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)

    browser.get(url)

    time.sleep(2)

    # get cap  value from page source and wait for element is present
    cap = WebDriverWait(browser, 10).until(
        EC.element_to_be_clickable((By.XPATH,
                                    '//*[@id="__layout"]/div/div[3]/main/div[2]/div/div/div[1]/sal-components/section/div/div/div[1]/div/div[2]/div/div/div/div[2]/ul/li[7]/div/div[2]')))
    cap_value = cap.text
    print(cap_value)

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 selenium 和 python 抓取的实时网站

Selenium Python 没有从这个网站上抓取

使用 BeautifulSoup 和 Selenium 的网页抓取网站不会检测网页中的表格元素

如何使用 Selenium 和 Python 从 youtube 上抓取网页

使用Python登录网站和网页抓取

在 Python 中使用 Selenium 进行网页抓取

使用 selenium python 从网页中抓取图像?

使用Python / Selenium抓取网页的内容

使用 BeautifulSoup 和 Selenium 抓取一个网站的多个网页的内容

Python:为什么Selenium没有使用Regex抓取循环中的最后一个网页?

AttributeError - 网页抓取 - Python - Selenium

Python Selenium 网页抓取没有可用的登录元素。手动登录的暂停脚本

使用 selenium 的网页抓取表仅获取 html 元素但没有内容

使用 Selenium 和 BeautifulSoup 进行 Zillow 网页抓取

使用 Selenium 和 BeautifulSoup 进行网页抓取返回空列表

python selenium 网页抓取。如何在网络中断或抓取的网站缓慢时继续重试?

使用 Selenium 和 Python 查找网页上的所有元素

使用 python 下载嵌入的 vimeo 视频但没有 selenium

使用Selenium和python在网页网格内抓取javascript数据

如何从使用React.js和Selenium在Python中的网页中抓取数据?

无法在使用 selenium 和 python 的网页抓取期间识别唯一元素

使用 python 和 selenium 进行网页抓取时与 JavaScript 对象交互

使用 selenium 进行 Python 网页抓取:在模式框中查找元素并在循环中下载它

使用 Python Selenium 性能进行网页抓取

使用Selenium Python(NSFW)从网页中抓取URL

如何使用Selenium(Python)网页抓取多个页面

即使在使用 selenium python 抓取数据之前网页也已关闭

在 python 网页抓取中使用 Selenium 对 BeautifulSoup 进行分页

使用 Selenium&gChrome 进行 Python 网页抓取