解析生成的网页 Python Selenium

Posted

技术标签:

【中文标题】解析生成的网页 Python Selenium【英文标题】:Parse resulting webpage Python Selenium 【发布时间】:2014-10-10 23:27:45 【问题描述】:

我在 Python 中使用 Selenium webdriver 在搜索字段中输入一些文本并查找它。我现在想解析该页面/在其上使用 BeautifulSoup 之类的东西。但是我对如何调用结果页面感到困惑。

到目前为止我的代码:

textinput = open("1.txt", "r").read()
url = "http://www.example.com"
driver = webdriver.Chrome(executable_path='path/chromedriver.exe')
driver.get(url)
sbox = driver.find_element_by_name("a")
sbox.send_keys(textinput)

submit = driver.find_element_by_xpath('//*[@id="maincontent"]/form/input[5]')
submit.click()

【问题讨论】:

当您输入文本时,然后用鼠标单击“go”、“search”等按钮或打开结果页面的按钮。然后您可以使用该页面的源代码来提取数据。请分享您的代码 @Vipul 请找到添加的代码。我能够得到结果。但我需要解析它。我无法手动复制结果页面的 URL,因为我会这样做数百次。我想自动化它。 使用driver.source可以获得源码 您也不需要获取源代码或使用 beautifulsoup,使用 selenium 驱动程序对象本身进行解析就足够了,并且通过使用 driver.find_element_by_<id,xpath...> 或 'driver.find_elements_by_ 中的 class、id、tag、xpath 非常干净' 【参考方案1】:

点击提交按钮后,使用:

submit.click()

它会自动转到下一页。因此,要解析结果页面,只需创建另一个:

whatimlookingfor = driver.find_element_by_id("myid")

submit = driver.find_element_by_xpath('//*[@id="maincontent"]/form/input[5]')
# You are still on the first page
submit.click()
# You are now on the second page
whatimlookingfor = driver.find_element_by_id("myid")

【讨论】:

以上是关于解析生成的网页 Python Selenium的主要内容,如果未能解决你的问题,请参考以下文章

js的网页爬虫爬不到吗

如何用python爬取js动态生成内容的页面

如何在scrapy框架下用python爬取json文件

.Net(C#) 对网页加载后解析后的内容截图或生成pdf文件的方法

Python网页解析器使用实例详解

python 用啥模块处理网页的javascript