新闻详情

News Information

Python真实兼职实战-京东商品信息海量获取
2024/12/18 20:58:17

Python真实兼职实战-京东商品信息海量获取#导入请求模块import osimport requests

Python真实兼职实战-京东商品信息海量获取

#导入请求模块


import os

import requests

count = 1 # 计数!

page = 0

# 1页

if not os.path.exists('评论数据'):

os.makedirs('评论数据')#用计数变量充当文件夹名称

# 伪装

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML,liket

while True:

# 网址 找到商品的页面开发者工具网络刷新页面点击商品评论放大镜里面搜关键词点击进去网址

url = f'https://api.m.jd.com/?appid=item-v3&functionId=pc_club_productPageComments&client=pc&clie‘

# 请求

res = requests.get(url,headers=headers)

# 数据的基本处理

JSON = res.json() # JSON

comments = JSON['comments'] # JSON里面的 comments里面有若干条评论数据

for comment in comments:

if not os.path.exists(f'评论数据/{count}'):

os.makedirs(f'评论数据/{count}') # 用计数变量充当文件夹名称

# 评论的文字

content = comment['content']# 在一条评论comment里面的content里面评论的文字。

print(content)

open(f'评论数据/{count}/content.txt', 'w', encoding='utf-8').write(content)

# 评论的图片

try:

images = comment['images']# 在一条评论comment里面的images里面

有若干张图片数据

for image in images:

imgUrl = 'https:' + image['imgUrl']

imgUrl = imgUrl.replace('n0/s128x96_jfs', 'shaidan/s616x405_jfs')

print('图片链接:',imgUrl)

res1 = requests.get(imgurl)

open(f'评论数据/{count}/{imgUrl.split("/")[-1]}','wb').write(res1.content)

except:

print('此评论没有图片!!!!!!!!')

# 评论的视频

try:

videos = comment['videos'] # 在一条评论comment里面的videos里面有若干个视频!

for video in videos:

videoUrl = video['remark']

print('视频链接:',videourl)

res2 = requests.get(videourl)

open(f'评论数据/{count}/{videoUrl.split("?")[0].split("/")[-1]}', 'wb').write(res2.con

except:

print('此评论没有视频!!!!!!!!')

count += 1

print('\n=====')

page += 1