for循环爬取评论数据

import time

import requests

import json

import openpyxl

'''#for循环

for i in range(3):

print(i)

time.sleep(0.1)

'''

wk=openpyxl.Workbook()

sheet=wk.create_sheet()

resp=requests.get('https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100006079301&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1')

content=resp.text

#对获取的数据格式处理,符合json的格式

rest=content.replace('fetchJSON_comment98(','').replace(');','')

#自己定义json

rest='{"comments":[{"productName":"产品一号","productColor":"白色"},{"productName":"产品二号","productColor":"红色"}]}'

print(rest)

json_data=json.loads(rest)

comments=json_data['comments']

for item in comments:

productName=item['productName']

productColor=item['productColor']

sheet.append([productName,productColor])

wk.save('D:/桌面文件/抓取的评论数据.xlsx')

(0)

相关推荐