从网页抓取数据(Vanguard延迟加载页面上的数据,我不知道如何正确获取数据 )
优采云 发布时间: 2021-11-14 13:03从网页抓取数据(Vanguard延迟加载页面上的数据,我不知道如何正确获取数据
)
我试图在这个网站上检查一些关于这个问题的问题,但我无法使他们的解决方案起作用。我正在使用 python 和 selenium 和 chrome 无头浏览器从先锋获取债券数据。Vanguard懒惰地加载页面上的数据,我不知道如何正确获取数据。
我正在尝试从此页面加载数据,尤其是基金事实表中的数据
当我像往常一样尝试这样做时,我得到
所以我尝试使用这行代码让浏览器等待数据加载。
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "data-ng-class")))
我确定这是在正确的轨道上,但我不知道如何正确地告诉我哪些元素应该等待识别以及我是否正确执行。有没有办法让我等到 iframe data-delayed-src 元素消失才能获取数据?
我已经看到它与 By.ID 的用法,但是我在数据 html 中没有看到任何我想要 id 的元素。
这是我正在使用的代码
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import os
dirname = os.path.dirname(__file__)
options = webdriver.ChromeOptions()
options.add_argument('--headless')
browser = webdriver.Chrome(options=options, executable_path=os.path.join(dirname, 'chromedriver'))
symbol = 'vbirx'
url_vanguard = 'https://investor.vanguard.com/mutual-funds/profile/overview/{}'
browser.get(url_vanguard.format(symbol))
# WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "data-ng-class")))
html = browser.page_source
mySoup = BeautifulSoup(html, 'html.parser')
htmlData = mySoup.find('table',{'role':'presentation'})
table = htmlData.find('tbody')
print('table: \n',table)
该表打印出缺少我想要的所有数据