python-3.x - 抓取 Quora 时无法提取 JavaScript 元素
问题描述
我正在尝试使用 Python、BeautifulSoup 和 Selenium 从 Quora 中提取数据以进行分析。但我无法提取页面上的 JavaScript 元素。我应该如何提取它们?
在这里,我只是试图提取 Quora 个人资料的简历,但我没有收到点击“更多”按钮后出现的文本。
# 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
解决方案
请使用下面的代码行首先单击“更多”按钮,然后获取配置文件的扩展文本。
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;
推荐阅读
- google-app-engine - 如何使用 Google API 在 App Engine 中添加自定义域映射?
- java - 如何在没有 Android Studio/XML 的情况下包含原始文件?
- android - 如何使用触摸/拖动移动对象?
- bash - 从外部覆盖命令
- javascript - javascript - 将事件和参数传递给使用 addEventListener 调用的函数
- python - Django 为所有网站访问者缓存整个呈现的 HTML 页面
- java - java api中的短语建议者整理查询
- javascript - 为什么我在画布中只得到一个圆圈?
- java - 如果添加现有数据,Java 中的 HashMap 内存使用量是多少
- r - 如何从 R 中的 nlrq 获取情节?