本页跳转 有的评论带图片 有的不带selenium定位不到text
解决办法:
转用requests+json( )解析
网页地址较长 从问号以及后面开始全部删除掉
设置param 和 headers
page为页数 设置for循环
【】中括号提取

完整代码如下:

import requests
import csv

url = ‘https://club.jd.com/comment/productPageComments.action

headers={
‘User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36’,
‘Referer’: ‘https://item.jd.com/‘,
‘Cookie’: ‘jdu=1241901770; areaId=3; shshshfpa=a5a305dd-bb96-1916-fc7a-b31ebcb8894c-1655442694; shshshfpb=f4ilu1kXi8Lp0YO-K4-kdAw; ipLoc-djd=3-51039-55931-0; PCSYCityID=CN_120000_120100_0; unpl=JF8EAJlnNSttDUhdVRhQSRYRGV8AWwkPQkdRaTNQXA1cTwEDSARORxF7XlVdXhRKEB9sYRRVVVNLXA4ZBCsSEXteU11bD00VB2xXXAQDGhUQR09SWEBJJV1QV10KQhUDam8FZG1bS2QFGjIbERVLVVVeWw5KJzNoVzVkWV1DXQUZMhoiEXsfAAJYAUsRB2wqBVdYWENVBR0EGiIRe14; jdv=76161171|haosou-search|t_262767352_haosousearch|cpc|15813831491_0_d28a2cc50c3e4e68ac7ed9a55d7b7dd0|1656292779086; jda=122270672.1241901770.1655442688.1656055850.1656292779.7; jdc=122270672; shshshfp=bab99c70da456b6bdb5f67c15c8c4030; ip_cityCode=51039; jwotest_product=99; token=507007c9a93b4563c758c8845876a0eb,2,920164; tk=rLbesIN3qLrgNiSnOfddNfKerAuEOiTeNfTdqDSDOLuDOUvfscvhqn,2,920164; jdb=122270672.13.1241901770|7.1656292779; shshshsID=b1c553dcfe116f8f261d4d59df523765_13_1656296853521; 3AB9D23F7A4B3C9B=KM5ANSXWEX652GOWYRATKIUPOOIU2GUIV5356YCIEWOHY6WXKDDK7LIV55T7WS7KVF64RSYQAZDDTREP7GIWEVXG3U; JSESSIONID=E4F5B92FC989F12E9F38839862B86A01.s1’
}
with open(‘京东黑丝评论.csv’, ‘a’, encoding=’utf-8’, newline=’’) as f:
csv_writer = csv.writer(f)
csv_writer.writerow([‘用户名称’, “购买款式”, ‘码数’,’评论内容’,’评论时间’])

for i in range(0,10,1):
param = {
“callback”: “”,
“productId”: “100019679118”,
“score”: 0,
“sortType”: 5,
“page”: i,
“pageSize”: 10,
“isShadowSku”: 0,
“rid”: 0,
“fold”: 1
}

  1. respones = requests.get(url=url, headers=headers,params=param)<br /> data = respones.json()<br /> # print(data)<br /> comments = data['comments']<br /> for comment in comments:<br /> yonghumc = comment['nickname']<br /> goumaikuanshi = comment['productColor']<br /> mashu = comment['productSize']<br /> pinglunneirong = comment['content']<br /> pinglunshijian = comment['creationTime']<br /> with open('京东黑丝评论.csv', 'a', encoding='utf-8', newline='') as f:<br /> csv_writer = csv.writer(f)<br /> csv_writer.writerow([yonghumc, goumaikuanshi, mashu, pinglunneirong,pinglunshijian])