python3[爬虫实战] 使用selenium,xpath爬取京东手机(下)

来源:互联网 发布:三体电影知乎 编辑:程序博客网 时间:2024/06/07 13:46

这次主要是进行京东具体某个店铺手机评论内容的爬取。

本来是跟上一起写的,只是没有时间一块做总结,现在写上来是有点生疏了。这里是暂时获取一个商品的评论内容

爬取的字段内容.png


爬取的字段:评论内容,购买机型,评论人

上代码:

# -*- 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
原创粉丝点击