首页 > 解决方案 > 抓取 Quora 时无法提取 JavaScript 元素

问题描述

我正在尝试使用 Python、BeautifulSoup 和 Selenium 从 Quora 中提取数据以进行分析。但我无法提取页面上的 JavaScript 元素。我应该如何提取它们?

在这里,我只是试图提取 Quora 个人资料的简历,但我没有收到点击“更多”按钮后出现的文本。

~ https://imgur.com/a/fTmeh1m

                # Extracting Bio
                driver.find_element_by_class_name('ui_qtext_more_link').send_keys(Keys.ENTER)
                bio = driver.find_element_by_class_name("ui_qtext_rendered_qtext").text

标签: python-3.xselenium-webdriverbeautifulsoupweb-crawlerquora

解决方案


请使用下面的代码行首先单击“更多”按钮,然后获取配置文件的扩展文本。

import time
//Fetch the more button element first
WebElement moreButton = driver.find_element_by_xpath("(//a[@class='ui_qtext_more_link'])[1]");    
//Click on the more button
moreButton.click();
time.sleep(3)
//Fetch the profileInfo element
WebElement profileInfo = driver.find_element_by_xpath("(//div[contains(@id,'expanded_content')]//span[@class='ui_qtext_rendered_qtext'])[1]");
//Store the bio in a string and use it further
String profileInfoBio = profileInfo.text;

推荐阅读