python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)
来源:互联网 发布:软件模块接口参数 编辑:程序博客网 时间:2024/06/08 15:13
这次主要是进行京东具体某个店铺手机评论内容的爬取。
本来是跟上一起写的,只是没有时间一块做总结,现在写上来是有点生疏了。这里是暂时获取一个商品的评论内容
爬取的字段:评论内容,购买机型,评论人
上代码:
# -*- coding: utf-8 -*-# @Time : 2017/9/18 23:16# @Author : 蛇崽# @Email : 17193337679@163.com# @File : TaoBaoZUK1Detail.py zuk z1 详情页内容import timefrom selenium import webdriverfrom lxml import etreechromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"browser = webdriver.Chrome(chromedriver)# 获取第一页的数据def gethtml(): url = "https://detail.tmall.com/item.htm?id=531993957001&skuId=3609796167425&user_id=268451883&cat_id=2&is_b=1&rn=71b9b0aeb233411c4f59fe8c610bc34b" browser.get(url) time.sleep(5) browser.execute_script('window.scrollBy(0,3000)') time.sleep(2) browser.execute_script('window.scrollBy(0,5000)') time.sleep(2) # 累计评价 btnNext = browser.find_element_by_xpath('//*[@id="J_TabBar"]/li[3]/a') btnNext.click() html = browser.page_source return htmldef getcomments(html): source = etree.HTML(html) commens = source.xpath("//*[@id='J_TabBar']/li[3]/a/em/text()") print('评论数一:',commens) # 将评论转为int类型 commens = (int(commens[0]) / 20) + 1 # 获取到总评论 print('评论数:',int(commens)) return int(commens)# print(html)def parseHtml(html): html = etree.HTML(html) commentlist = html.xpath("//*[@class='rate-grid']/table/tbody") for comment in commentlist: # 评论 vercomment = comment.xpath( "./tr/td[@class='tm-col-master']/div[@class='tm-rate-content']/div[@class='tm-rate-fulltxt']/text()") # 机器类型 verphone = comment.xpath("./tr/td[@class='col-meta']/div[@class='rate-sku']/p[@title]/text()") print(vercomment) print(verphone) # 用户(头尾各一个字,中间用****代替) veruser = comment.xpath("./tr/td[@class='col-author']/div[@class='rate-user-info']/text()") print(veruser) print(len(commentlist))# parseHtml(html)# print('*'*20)def nextbuttonwork(num): if num != 0 : browser.execute_script('window.scrollBy(0,3000)') time.sleep(2) # browser.find_element_by_css_selector('#J_Reviews > div > div.rate-page > div > a:nth-child(6)').click() try: browser.find_element_by_css_selector('#J_Reviews > div > div.rate-page > div > a:last-child').click() # browser.find_element_by_xpath('//*[@id="J_Reviews"]/div/div[7]/div/a[3][contains(text(), "下一页")]').click() except: pass # browser.find_element_by_xpath('//*[@id="J_Reviews"]/div/div[7]/div/a[3][contains(text(), "下一页")]').click() time.sleep(2) browser.execute_script('window.scrollBy(0,3000)') time.sleep(2) browser.execute_script('window.scrollBy(0,5000)') time.sleep(2) html = browser.page_source parseHtml(html) print('nextclick finish ')def selenuim_work(html): print('selenuim start ... ') parseHtml(html) nextbuttonwork(1) print('selenuim end....') passdef gettotalpagecomments(comments): html = gethtml() for i in range(0,comments): selenuim_work(html)data = gethtml()# 得到评论commens = getcomments(data)# 根据评论内容进行遍历gettotalpagecomments(commens)
这里头还是好的
不足:
这里主要进行了单页的爬取, 下一页的按钮还是没有获取到,不知道为什么获取不到,可能是axaj的原因吧, 另外想说一下大公司确实tm牛, 当然了作为爬虫工程师,这在工作中是不可避免的。还麻烦写京东商品评论的帮忙指导一下小白。
阅读全文
0 0
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(上)
- python3 [爬虫入门实战]爬虫之selenium 安装设置与初步使用
- python3 [爬虫入门实战] 爬虫之使用selenium 爬取百度招聘内容并存mongodb
- python3 [爬虫实战] 微博爬虫京东客服之Selenium + Chrom浏览器的使用(上)
- python3 [入门基础实战] 爬虫入门之xpath的学习
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3爬虫实战(3)
- python3 [爬虫入门实战] 爬虫之selenium 模拟QQ登陆抓取好友说说内容(暂留)
- 【Python3.6爬虫学习记录】(七)使用Selenium+ChromeDriver爬取知乎某问题的回答
- Selenium XPATH使用
- Selenium中使用XPath
- selenium -- Xpath 使用
- Python3爬虫实战之爬取京东图书图片
- python3 [入门基础实战] 爬虫入门之xpath爬取脚本之家python栏目
- Python3爬虫学习笔记(5.Selenium简单操作介绍)
- 第4周项目5(2) 循环双链表应用
- 移动架构18_ListView源码分析
- hihoCoder 1054:滑动解锁(DFS)
- java IO笔记(DataInputStream/DataOutputStream)
- 双重检查模式 (DCL)与单例模式
- python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)
- JQuery this和$(this)的区别及获取$(this)子元素对象的方法
- bootstrap学习笔记心得1
- 如何防止主机被ping
- GCC Command Options
- 单链表
- C语言分析——文件操作
- CodeForces 867C Ordering Pizza
- 搜集来的命令集合,挺好的