使用 Selenium 和 Python 抓取 Morningstar 网站。 Selenium 不下载完整的网页
Using Selenium and Python to scrape Morningstar website. Selenium doesn't download the full webpage
这是我的代码:
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() 的输入,它可以工作,但我想自动化。
试试这个
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)
这是我的代码:
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() 的输入,它可以工作,但我想自动化。
试试这个
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)