這次主要是進行京東具體某個店鋪手機評論內(nèi)容的爬取田盈。
本來是跟上一起寫的,只是沒有時間一塊做總結(jié)缴阎,現(xiàn)在寫上來是有點生疏了允瞧。這里是暫時獲取一個商品的評論內(nèi)容
爬取的字段:評論內(nèi)容,購買機型蛮拔,評論人
上代碼:
# -*- coding: utf-8 -*-
# @Time : 2017/9/18 23:16
# @Author : 蛇崽
# @Email : 17193337679@163.com
# @File : TaoBaoZUK1Detail.py zuk z1 詳情頁內(nèi)容
import time
from selenium import webdriver
from lxml import etree
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
browser = webdriver.Chrome(chromedriver)
# 獲取第一頁的數(shù)據(jù)
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 html
def getcomments(html):
source = etree.HTML(html)
commens = source.xpath("http://*[@id='J_TabBar']/li[3]/a/em/text()")
print('評論數(shù)一:',commens)
# 將評論轉(zhuǎn)為int類型
commens = (int(commens[0]) / 20) + 1
# 獲取到總評論
print('評論數(shù):',int(commens))
return int(commens)
# print(html)
def parseHtml(html):
html = etree.HTML(html)
commentlist = html.xpath("http://*[@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....')
pass
def gettotalpagecomments(comments):
html = gethtml()
for i in range(0,comments):
selenuim_work(html)
data = gethtml()
# 得到評論
commens = getcomments(data)
# 根據(jù)評論內(nèi)容進行遍歷
gettotalpagecomments(commens)
這里頭還是好的
不足:
這里主要進行了單頁的爬取, 下一頁的按鈕還是沒有獲取到建炫,不知道為什么獲取不到畦韭,可能是axaj的原因吧, 另外想說一下大公司確實tm牛肛跌, 當(dāng)然了作為爬蟲工程師艺配,這在工作中是不可避免的。還麻煩寫京東商品評論的幫忙指導(dǎo)一下小白衍慎。