无法使用 Selenium 和 BeautifulSoup 抓取文本

卡尔·埃米尔·图尔斯特鲁普

我正在尝试使用 Python 中的 Selenium 和 BeautifulSoup 从 Morningstar 自动获取研究项目的数据。我是 Python 新手,所以我刚刚尝试了 Stackoverflow 和类似论坛的一堆解决方案,但没有成功。

我想要抓取的是网址https://www.morningstar.dk/dk/funds/snapshot/snapshot.aspx?id=F000014CU8&tab=3在网址中,我专门寻找“Faktorprofil”您可以单击以将数据显示为表格。我可以从 url 获取标题,但我无法找到任何其他文本。我曾尝试使用多个 ID 和类,但没有任何运气。我相信我最成功的代码写在下面。我希望有人能帮帮忙!

from bs4 import BeautifulSoup
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opts = Options()
opts.add_argument(" --headless")



chrome_driver = os.getcwd() +"/chromedriver"


driver = webdriver.Chrome(options=opts, executable_path=chrome_driver)


driver.get("https://www.morningstar.dk/dk/funds/snapshot/snapshot.aspx?id=F00000ZG2E&tab=3")


soup_file=driver.page_source
soup = BeautifulSoup(soup_file, 'html.parser')

print(soup.title.get_text())

#print(soup.find(class_='').get_text())
#print(soup.find(id='').get_text())

这是我要抓取的数据 [1]:https : //i.stack.imgur.com/wkSMj.png

帕马杜

所有这些表都在一个iframe. 下面的代码将检索数据并打印为列表。

driver.implicitly_wait(10)
driver.get("https://www.morningstar.dk/dk/funds/snapshot/snapshot.aspx?id=F000014CU8&tab=3")
driver.switch_to.frame(1)
driver.find_element_by_xpath("//button[contains(@class,'show-table')]//span").click()
table = driver.find_elements_by_xpath("//div[contains(@class,'sal-mip-factor-profile__value-table')]/table//tr/th")
header = []
for tab in table:
    header.append(tab.text)
print(header)
tablebody = driver.find_elements_by_xpath("//div[contains(@class,'sal-mip-factor-profile__value-table')]/table//tbody/tr")
for tab in tablebody:
    data = []
    content = tab.find_elements_by_tag_name("td")
    for con in content:
        data.append(con.text)
    print(data)

输出:

['Faktorer', 'Fonds værdi', '5 år Min. Værdi', '5 år Maks værdi', 'Kategori Gennemsnitlig']
['Stil', '62,33', '31,52', '76,36', '48,20']
['Effektiv rente', '48,83', '20,82', '69,12', '34,74']
['Momentum', '58,47', '7,48', '77,21', '71,15']
['Kvalitet', '25,65', '21,61', '59,66', '38,15']
['Volatilitet', '45,25', '34,66', '81,08', '74,93']
['Likviditet', '35,70', '33,40', '74,94', '79,39']
['Størrelse', '39,60', '35,67', '48,78', '87,59']

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 BeautifulSoup 和 Selenium 抓取数据

如何使用 Python、Selenium 和 BeautifulSoup 从 HTML <span id> 中抓取此文本?

使用Selenium和BeautifulSoup输入内容来抓取网站?

使用 Selenium 和 BeautifulSoup 抓取饥饿游戏的用户评分

使用BeautifulSoup和Selenium抓取特定的html标签

使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

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

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

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

我无法使用BeautifulSoup抓取HTML文本

无法使用beautifulsoup抓取div文本

使用Selenium和python进行Web抓取-包含文本的xpath

如何使用 Selenium 和 BeautifulSoup 从标签中获取文本

使用 selenium 和 BeautifulSoup 抓取动态网页,但新页面不断弹出

使用来自 AJAX 网站的 selenium 和 beautifulsoup 在 python 中抓取图像

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

使用 beautifulsoup 和 selenium 抓取多页网站返回空字符串列表

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

无法使用 BeautifulSoup 定位元素和抓取内容

无法使用python和beautifulsoup抓取网页中的某些href

无法使用BeautifulSoup和Requests抓取下拉菜单

无法使用 beautifulsoup 和 requests 进行网络抓取

使用Python Beautifulsoup在Web上同时抓取html文本和图像链接

使用 BeautifulSoup 从抓取的页面中提取文本

使用BeautifulSoup抓取网站后缺少文本

使用BeautifulSoup抓取预标签内的文本

使用Selenium和BeautifulSoup搜寻网站

如何使用 BeautifulSoup 和 Selenium 实现 if 语句

使用Selenium和BeautifulSoup的慢代码