当前位置:   article > 正文

爬虫ajax url不变,爬虫相关:关于使用selenium处理ajax翻页前后取出的数据没变的问题...

selenium爬url不变时

有一类页面,它的翻页是用ajax处理的,也就是翻页前后url没有发生变化,如果要爬这类页面,无法用传统的、获得url规则的方法,可以考虑使用selenium+phantomjs模拟鼠标点击翻页按钮。

但是模拟鼠标点击翻页之后,取出来的数据有可能没有发生变化(也就是说“点击”之后,第二次取出的数据和第一次取出的数据,比如标题,是一样的)。原因有可能是模拟点击鼠标的动作发生之后,程序还没有等到真正翻页完成就去取了第二次数据,导致取的还是第一页的数据。对于这种情况,解决办法就是在模拟鼠标点击的动作发生之后,休眠几秒钟再取数据,会比较保险。

以下是简单示例:

from selenium import webdriver

from lxml import etree

import time

current_url = "http://photo.nocutnews.co.kr/news/issue/list"

driver = webdriver.PhantomJS(executable_path="phantomjs")

driver.get(current_url)

# title = driver.find_element_by_xpath("//div[@class='photoinfo']/a/strong").text

ps = driver.page_source

html = etree.HTML(ps.encode("utf-8"))

title = html.xpath("//div[@class='photoinfo']/a/strong/text()")

print title

driver.execute_script("__doPostBack('ctl00$ctl00$cphBody$cphBody$pcPager$ctl03','')")

time.sleep(5)

# title = driver.find_element_by_xpath("//div[@class='photoinfo']/a/strong").text

ps1 = driver.page_source

html1 = etree.HTML(ps1.encode("utf-8"))

title1 = html1.xpath("//div[@class='photoinfo']/a/strong/text()")

print title1

driver.quit()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/489051
推荐阅读
相关标签
  

闽ICP备14008679号