利用selenium爬取Axaj和动态网页
来源:互联网 发布:filco 圣手二代 mac 编辑:程序博客网 时间:2024/06/06 09:59
前言
我们知道python爬虫,一般小爬虫requests+beautifulSoup,分布式爬虫用Scrapy,我们一般用requests来的方便,但是有的时候你会发现有些网页信息(比如用户评论),用requests是无法获取的,这些javascript管理的动态内容怎样提取?
这就引入我们今天的主角selenium。
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是一种异步加载技术。
在前面文章python爬取CSDN博客并用WordCloud词云分析中,要提取的内容是直接从网页的source code里拿到的。但是一些Ajax动态内容是在source code找不到的,就要找合适的程序库把异步或动态加载的内容加载上来,交给本项目的提取器进行提取。
python可以使用selenium执行javascript,selenium可以让浏览器自动加载页面,获取需要的数据。selenium自己不带浏览器,可以使用第三方浏览器如Firefox,Chrome等,也可以使用headless浏览器如PhantomJS在后台执行。
用selenium爬取应用宝用户评论
- 首先我们需要下载phantoms,我们下载的是压缩包,解压一下就好了。
- 编写代码:
url = 'http://sj.qq.com/myapp/detail.htm?apkName=com.baidu.lbs.waimai' driver = webdriver.PhantomJS(executable_path='F:\\大数据文件\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe') #这里是你自己的phantomjs的解压路径driver.get(url) Comment=[]Score=[]Date=[]Name=[]time.sleep(2) for i in range(20): driver.find_element_by_id("J_DetCommentShowMoreBtn").click()score=driver.find_elements_by_xpath("//div[@class='comment-name-line']/div[2]")name=driver.find_elements_by_xpath("//div[@class='comment-name']")date=driver.find_elements_by_xpath("//div[@class='comment-date']")comment=driver.find_elements_by_xpath("//div[@class='comment-datatext']")for j in range(len(name)): Comment.append(comment[j].text) Score.append(score[j].get_attribute('class')) Date.append(date[j].text) Name.append(name[j].text)data={'name':Name,'score':Score,'date':Date,'comment':Comment}table=pd.DataFrame(data)table.to_excel("C:\\Users\\user\\Desktop\\app2.xlsx")
上面代码如果用过爬虫的还是很好理解的,就是用xpath来匹配,xpath语法可以参考这里
最后得到结果:
广告请自行忽略0.0
我们发现评分一栏我们抓取的是源码,还需要用正则表达式处理一下。
score=[]for line in data1: score.extend(re.findall(r"\d+",line))
好了,到这里我们抓取已经成功了!
阅读全文
0 0
- 利用selenium爬取Axaj和动态网页
- selenium+ phantomjs实现动态网页爬取
- selenium+ Phantomjs爬取动态网页
- [Python爬虫]Scrapy配合Selenium和PhantomJS爬取动态网页
- 动态网页爬取例子(WebCollector+selenium+phantomjs)
- selenium+webdriver爬取动态网页介绍_python
- 用python+selenium+firefox爬取动态网页
- 基于Python3的phantomJs+Selenium动态网页爬取技术
- python+selenium+PhantomJS爬取网页动态加载内容
- 使用selenium等待网页加载完成,lxml解析网页,利用urllib爬取图片
- 利用htmlunit和jsoup来实现爬取js的动态网页
- 使用selenium动态爬取
- python爬取静态和动态网页
- 基于Python,Selenium和PhantomJS实现动态页面爬取
- Selenium学习三——利用Python爬取网页表格数据并存到excel
- 使用selenium的webdriver动态爬取网页时遇到的问题
- webmagic+selenium模拟浏览器启动(动态网页爬取方法之一的第一步)
- python_利用selenium 爬取淘宝商品
- 历史版本的cuda和cuDNN
- 二维数组
- 团体程序设计天梯赛-练习集 L1-034. 点赞
- 连分数与历法
- 做数学题比统一世界更爽,你会怎么做呢?
- 利用selenium爬取Axaj和动态网页
- 炫酷的环形调节器控件 : RegulatorView
- 移动硬盘I盘误删的数据恢复方法
- 吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(4-4)-- 特殊应用:人脸识别和神经风格迁移
- Ubuntu上用Hexo搭建博客托管到github
- 分答项目_知识点:php://input $POST $HTTP_RAW_POST_DATA三者之间的关系
- 如果你们都忘记了鼓励原创,那,我们来
- python朴素贝叶斯实现-1( 贝叶斯定理,全概率公式 )
- 通过java实现txt,ppt,pdf上传之后转换为swf文件的功能实现