直接上代码 import csv import random import requests #appid可以替换成你要爬取的app的appid url='https://web-drcn.hispace.dbankcloud.com/edge/uowap/index?method=internal.user.commenList3&serviceType=20&reqPageNum=%s&maxResults=25&appid=C10219077&version=10.0.0&zone=&locale=zh' def get_agent(): agents= ['Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;', 'Mozilla/5.0 (Macintosh; Intel Mac os x 10.6; rv,2.0.1) Gecko/20100101 Firefox/4.0.1', 'Opera/9.80 (Macintosh; Intel Mac os x 10.6.8; U; en) Presto/2.8.131 Version/11.11', 'Mozilla/5.0(Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11(KHTML,like Gecko)Chrome/17.0.963.56Safari/535.11', 'Mozilla/4.0 (compatible; MsIE 7.0; Windows NT 5.1; 360SE)'] fakeheader = {} a=random.randint(0,len (agents)-1) fakeheader['user-agent'] = agents[a] return fakeheader csv_file = '../csv/华为应用商店评论.csv' for i in range(5,10): with open(csv_file, mode='a', newline='', encoding='utf-8') as file: writer = csv.writer(file) response = requests.get(url%i,timeout=30,headers=get_agent()) # 检查请求是否成功 if response.status_code == 200: # 解析HTML内容 comment_list=response.json()["list"] if comment_list ==[]: break for comment in comment_list: aa=comment["commentInfo"] #爬取到的评论写入csv文件 writer.writerow(["智能证件照",aa]) else: break
爬虫入门-爬取华为应用商店app的评论
最新推荐文章于 2024-12-04 12:09:38 发布