【发布时间】:2016-01-11 11:09:27
【问题描述】:
我正在尝试抓取 Quora 的答案。 例如考虑这个链接: https://www.quora.com/Is-it-too-late-for-an-X-year-old-to-learn-how-to-program
看第一个答案,有一个“更多”标签,表示存在更多隐藏的文本。
问题 在 Python 中使用 Selenium 我无法打开并从该项目中提取文本。
我试试…… 我还尝试使用 JavaScript 单击此指令的“显示更多”链接:
try:
if ans.find_element_by_xpath('.//a[contains(@class, "more_link")]').is_displayed():
elem_more = ans.find_element_by_xpath('.//a[contains(@class, "more_link")]')
#self.driver.execute_script("arguments[0].click();", elem_more )
Hover = webdriver.ActionChains(self.driver).move_to_element(elem_more)
Hover.click(elem_more).perform()
#wait_1.until(EC.invisibility_of_element_located((By.CLASS_NAME, "switch_indicator")))
except (NoSuchElementException,TimeoutException) as e:
pass
然后用
获取答案的内容 content = ans.find_element_by_xpath('.//span[contains(@class, "inline_editor_value")]')
这适用于没有“更多”的答案,因为它是我用来从任何答案中提取文本的容器。
【问题讨论】:
标签: javascript python css selenium web-scraping