更新:这是2020年的参数,已经不能直接使用。
利用Ajax爬取淘宝评论,这里完整的补充一下,包括数据存储。
对于Ajax参数的分析,Python爬取平台评论,这篇文章分析过了这里不再重复了。
主要是完善一下代码。
import time
import requests
import csv
import json
import os
import random
class taobaoSpider_content():
"""通过分析网址的Ajax获取淘宝商品评论
其中get_page方法是构造出url并且返回json格式的文本数据,get_cotent
方法用于提取数据并返回一个生成器,最后main方法整合输出结果
Attributes: itemId:商品ID
Attributes:currentPage:评论页码
Attributes:sellerId:淘宝商家Id
"""
def __init__(self,itemId,sellerId,currentPage):
self.currentPage=currentPage
self.url="https://rate.tmall.com/list_detail_rate.htm?"
self.headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400",
"referer":"https://detail.tmall.com/item.htm",
"Cookie":'自己的Cookie'
}
self.itemId=itemId
self.sellerId=sellerId
def get_page(self):
t_param=time.time()
t_list=str(t_param).split(".")
params={ "sellerId":self.sellerId,
"itemId":self.itemId,
"callback":str(int(t_list[1][3:])+1),
"_ksTS":t_list[0]+t_list[1][:3]+"_"+t_list[1][3:],
"currentPage":self.currentPage
}
res=requests.get(self.url,params=params,headers=self.headers)
try:
if res.status_code==200:
res=requests.get(self.url,params=params,headers=self.headers).text[len(t_list[1][3:])+3:-1]
res_json=json.loads(res)
res_str=json.dumps(res_json,indent=4)
return json.loads(res_str)
except:
return None
def get_content(self,json_data):
if json_data!=None:
for item in json_data.get("rateDetail").get("rateList"):
content_time=item.get("rateDate")
content_type=item.get("auctionSku")
content_name=item.get("displayUserNick")
content_data=item.get("rateContent")
yield {
"content_time":content_time,
"content_type":content_type,
"content_name":content_name,
"content_data":content_data,
}
else:
print("该页出错啦!")
return None
def write_txt(self,data):
"""这是将结果写入txt文本文档的格式
将字典写入文本文档首先要利用json.dumps()转换成字符串格式.
json.dumps的indent参数是为了美化输出
json.ensure_ascii参数将数据输出程中文,同时要规定文件输出编码为utf-8
"""
with open("taobaocontent.txt","a",encoding="utf-8") as file:
file.write(json.dumps(data,indent=2,ensure_ascii=False))
file.write("\n")
def write_csv(self,data):
"""这是将结果写入csv文件
将字典直接写入csv需要用到csv模块。具体可以参考相关文档
这里要注意写入csv容易出现中文乱码,需要加encoding和newline参数。
"""
with open("taobaocontent.csv","a",encoding="utf-8-sig",newline='') as file:
fieldnames=["content_time","content_type","content_name","content_data"]
writer=csv.DictWriter(file,fieldnames=fieldnames)
writer.writerow(data)
def main(self):
json_data=self.get_page()
self.get_content(json_data)
return self.get_content(json_data)
if __name__=="__main__":
for i in range(1,2):
new_data=taobaoSpider_content(itemId=602210068322,sellerId=2616970884,currentPage=i)
if new_data.main()!=None:
for items in new_data.main():
new_data.write_txt(items)
else:
pass
time.sleep(random.randint(5,10)) #设置延时防止爬虫被封
以上代码已成功运行,如有错误,烦请告知,谢谢!