首页 > 解决方案 > 仅从使用 selenium python 的文章中获得第一段(需要所有段落)

问题描述

我想从这篇文章中提取所有段落,但我设法只使用 selenium for python 获得了第一段。文章链接为: https ://nthqibord.com/2019/08/15/pemimpin-pkr-pertahan-tun-mahathir/

我这样做是为了练习,但无法提取整篇文章。

我已经尝试了下面的代码来提取段落的确切部分:

post = driver.find_element_by_xpath("//div[@class='td-ss-main-content']/div[@class='td-post-content']//p")

结果只得到了第一段。我需要所有的段落。

标签: python-3.xseleniumxpathcss-selectorswebdriverwait

解决方案


list = [p.text for p in self.driver.find_elements_by_xpath("//div[@class='td-ss-main-content']/div[@class='td-post-content']//p")]
paragraphs = "\n  ".join(map(str, list))

推荐阅读