天天看点

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

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

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

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

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

上代码:

# -*- coding: utf-8 -*-
# @Time    : 2017/9/18 23:16
# @Author  : 蛇崽
# @Email   : [email protected]
# @File    : TaoBaoZUK1Detail.py zuk z1 详情页内容

import time
from selenium import webdriver
from lxml import etree

chromedriver = "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()
    browser.execute_script('window.scrollBy(0,3000)')
    time.sleep()
    browser.execute_script('window.scrollBy(0,5000)')
    time.sleep()

    # 累计评价
    btnNext = browser.find_element_by_xpath('//*[@id="J_TabBar"]/li[3]/a')
    btnNext.click()
    html = browser.page_source
    return html



def getcomments(html):
    source = etree.HTML(html)
    commens = source.xpath("//*[@id='J_TabBar']/li[3]/a/em/text()")
    print('评论数一:',commens)
    # 将评论转为int类型
    commens = (int(commens[]) / ) + 
    # 获取到总评论
    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 !=  :
        browser.execute_script('window.scrollBy(0,3000)')
        time.sleep()
        # 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()
        browser.execute_script('window.scrollBy(0,3000)')
        time.sleep()
        browser.execute_script('window.scrollBy(0,5000)')
        time.sleep()
        html = browser.page_source
        parseHtml(html)
        print('nextclick finish  ')


def selenuim_work(html):
    print('selenuim start ... ')
    parseHtml(html)
    nextbuttonwork()
    print('selenuim  end....')
    pass


def gettotalpagecomments(comments):
    html = gethtml()
    for i in range(,comments):
        selenuim_work(html)

data = gethtml()
# 得到评论
commens = getcomments(data)
# 根据评论内容进行遍历
gettotalpagecomments(commens)
           

这里头还是好的

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

不足:

这里主要进行了单页的爬取, 下一页的按钮还是没有获取到,不知道为什么获取不到,可能是axaj的原因吧, 另外想说一下大公司确实tm牛, 当然了作为爬虫工程师,这在工作中是不可避免的。还麻烦写京东商品评论的帮忙指导一下小白。