前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium在异步加载中的应用简书文章异步加载Selenium代码代码分析

Selenium在异步加载中的应用简书文章异步加载Selenium代码代码分析

作者头像
罗罗攀
发布2018-07-03 14:12:45
1.4K0
发布2018-07-03 14:12:45
举报

简书文章异步加载

之前爬虫小分队的第一次作业就是爬取简书七日热门,同学们应该知道部分数据是异步加载的,对于阅读,评论,喜欢的抓取数据策略为使用正则表达式匹配,收录专题就是找包来获取数据的。

Selenium代码

from selenium import webdriver
url = 'http://www.jianshu.com/p/c9bae3e9e252'
def get_info(url):
    include_title =[]
    driver = webdriver.PhantomJS()
    driver.get(url)
    driver.implicitly_wait(20)
    author = driver.find_element_by_xpath('//span[@class="name"]/a').text
    date = driver.find_element_by_xpath('//span[@class="publish-time"]').text
    word = driver.find_element_by_xpath('//span[@class="wordage"]').text
    view = driver.find_element_by_xpath('//span[@class="views-count"]').text
    comment = driver.find_element_by_xpath('//span[@class="comments-count"]').text
    like = driver.find_element_by_xpath('//span[@class="likes-count"]').text
    included_names = driver.find_elements_by_xpath('//div[@class="include-collection"]/a/div')
    for i in included_names:
        include_title.append(i.text)
    print(author,date,word,view,comment,like,include_title)
get_info(url)

由于只搞了一个页面的,没有存入数据库,就打印了结果。

代码分析

由于selenium是加载了javascript的,所以我们用chrome浏览器,直接检查的xpath路径就能提取到信息,以收录专题为例,检查元素,来构造xpath路径,这样就不用找包啦。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.05.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简书文章异步加载
  • Selenium代码
  • 代码分析
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档