最近因需求需要,需要到京东爬取一些类别的商品信息。记录下过程中踩过的坑,最后奉献上全部代码。仅供互相学习,如有错误请指正~~京东网页翻页。

京东的页面是打开时先加载前30个商品,浏览到下面时再加载另30个商品。加载前30个商品时 page=1,后30个商品时 page=2。所以京东的翻页可以用request库直接 page+1翻页,也可以使用selenium库滚轮操作到最后全部加载完成后以page = 2n-1的方式翻页。

获取商品详情页的价格及评论

从以上代码获取到商品详情页的网页后,继续对详情url发起请求后发现请求不到商品价格及评论。最后用青花瓷抓包后发现这2个是单独的JS加载,返回的是两个json包。可以通过解析json获取价格和评论。

全部代码如下:

from bs4 import BeautifulSoup

import requests as re

import random,json

from selenium import webdriver

from time import sleep

from urllib.parse import quote

import pandas as pd

def Get_Header(re_url):

list_header = [

{

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',

'cache-control':'max-age=0',

'Cookie':'__jda=122270672.1744496012.1577155345.1598497068.1598508599.11; __jdu=1744496012; shshshfp=075cb5bfc884ea12ffa496ed64bd02f9; shshshfpa=c831f236-6ab3-aa6d-14d2-b2ef1c6bd04e-1588213838; shshshfpb=bF5VGKKJpLPVkXI1nPyFvHQ%3D%3D; unpl=V2_ZzNtbUEESxB1CRJRLklZB2JREV4RUkUcJQARVikYWQ1uAxZfclRCFnQUR11nGloUZwIZWURcRhJFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHsfWQBiCxVcQlRzJXI4dmR5H1kDZAsiXHJWc1chVE9SfR5ZAyoDFFhHUksSdAhFZHopXw%3d%3d; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_2b9410e4da434c22b478a9f3c0498153|1598497067783; areaId=18; ipLoc-djd=18-1482-48942-49058; PCSYCityID=CN_430000_430100_0; _pst=%E6%9E%AB%E5%8F%B68%E7%96%AF%E8%80%B6; unick=%E6%9E%AB%E5%8F%B68%E7%96%AF%E8%80%B6; pin=%E6%9E%AB%E5%8F%B68%E7%96%AF%E8%80%B6; _tp=BbPVbKtR38igbOb%2B6nMOFcitpL6scJlLuOEa4E7XiG%2BuT9kM0ukhvBamyGyg9b0W; __jdc=122270672; shshshsID=bdf848f07ecf3bdaadb0464a9365fbfd_6_1598509486237; user-key=ee732f60-2886-4e04-be53-d0157916bc02; cn=0; __jdb=122270672.6.1744496012|11.1598508599; 3AB9D23F7A4B3C9B=U3GDXXISZUYHKELVNFI4WVW3PWLZJCTYCKPU7HYZC3XUFR66FDWFKIXLECXK46FPOOCQUFAFPMN67RL3SJLQLWCZIU',

'upgrade-insecure-requests':'1',

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',

'Referer':f'{re_url}',

'Connection':'close'

}

]

return random.choice(list_header)

def Get_Product_Url(url):

driver = webdriver.Firefox()

driver.get(url)

js_code = '''window.scrollTo(0,5000)'''

driver.execute_script(js_code)

sleep(5)

soup = BeautifulSoup(driver.page_source,'lxml')

product_url_list = [f"https://item.jd.com/{tag.attrs['data-sku']}.html" for tag in soup.find_all('li',class_='gl-item')]

price_url_list = [f"https://p.3.cn/prices/mgets?&skuIds=J_{tag.attrs['data-sku']}" for tag in soup.find_all('li',class_='gl-item')]

comment_url_list = [f"https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&score=7&sortType=5&page=0&pageSize=10&isShadowSku=0&productId={tag.attrs['data-sku']}" for tag in soup.find_all('li',class_='gl-item')]

driver.close()

return product_url_list,price_url_list,comment_url_list

def Get_Product_Url_R(url):

headers = Get_Header(re_url='https://www.jd.com/')

response = re.get(url,headers = headers)

soup = BeautifulSoup(response.text,'lxml')

# 获取一页商品的 商品详情页URL,商品价格URL,商品评论URL的列表

product_url_list = [f"https://item.jd.com/{tag.attrs['data-sku']}.html" for tag in soup.find_all('li',class_='gl-item')]

price_url_list = [f"https://p.3.cn/prices/mgets?&skuIds=J_{tag.attrs['data-sku']}" for tag in soup.find_all('li',class_='gl-item')]

comment_url_list = [f"https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&score=7&sortType=5&page=0&pageSize=10&isShadowSku=0&productId={tag.attrs['data-sku']}" for tag in soup.find_all('li',class_='gl-item')]

sleep(random.randrange(1, 5))

return product_url_list,price_url_list,comment_url_list

def Get_Data_M(url,i):

product_url_list,price_url_list,comment_url_list = Get_Product_Url_R(url)

headers = Get_Header(url)

for j in range(1,len(product_url_list)):

Get_Data_S(product_url_list[j],price_url_list[j],comment_url_list[j],headers,i,j)

# if tag_list:

# for tag in tag_list:

# try:

# if product_url_list[i] != f"https://item.jd.com/{tag.attrs['data-sku']}.html": # 排除已选商品

# pro_url = f"https://item.jd.com/{tag.attrs['data-sku']}.html"

# pri_url = f"https://p.3.cn/prices/mgets?&skuIds=J_{tag.attrs['data-sku']}"

# com_url = f"https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&score=7&sortType=5&page=0&pageSize=10&isShadowSku=0&productId={tag.attrs['data-sku']}"

# Get_Data_S(pro_url,pri_url,com_url,headers,flag=False)

# except Exception as e:

# # print(e)

# pass

return result

def Get_Data_S(pro_url,pri_url,com_url,headers,i,j,flag = True):

# 获取商品标题

try:

response = re.get(pro_url, headers=headers,timeout=3)

soup = BeautifulSoup(response.text, 'lxml')

header_packet = soup.find('div', class_='itemInfo-wrap')

title = header_packet.find('div', class_='sku-name').text.replace(' ', '')

# 获取详情页其他规格商品的URL

# if flag:

# try:

# tag_list = header_packet.find_all('div', attrs = {'class':'item'})

# except:

# tag_list = 0

# 更换headers内的来源网站,更换为商品详情页,爬取价格及评论

headers = Get_Header(pro_url)

# 获取商品价格

response = re.get(pri_url, headers=headers)

price = (json.loads(response.text))[0]['p']

# 获取商品评论

response = re.get(com_url, headers=headers)

page = (response.text).replace('fetchJSON_comment98(', '').replace(');', '').replace(r'\n','')

comment_dict = json.loads(page)

# 全部评论数

commentCount = comment_dict['productCommentSummary']['commentCount']

# 好评数

goodcommentCount = comment_dict['productCommentSummary']['goodCount']

# 好评率

goodcommentRate = comment_dict['productCommentSummary']['goodRate']

print(f'{dept_name} 第{i}页第{j}个商品:',[title,price,commentCount,goodcommentCount,goodcommentRate])

result.append([title,price,commentCount,goodcommentCount,goodcommentRate])

sleep(random.randrange(1,3))

except Exception as e:

print(f'{dept_name} 第{i}页第{j}个商品爬取失败!')

print(e)

pass

# return tag_list

def Save_Data(data,dept_name,dept):

sheet = pd.DataFrame(data=data,columns=['商品标题','商品价格','评论数','好评数','好评率'])

sheet['小类名称'] = dept_name

sheet['小类'] = dept

sheet = sheet[['小类','小类名称','商品标题','商品价格','评论数','好评数','好评率']]

return sheet

def Main():

global result,dept_name

table = pd.DataFrame()

# Excel = pd.ExcelWriter(r'C:\Users\1000138836\Desktop\result.xlsx') # 结果输出文件

dept_xlsx = pd.read_excel(r'C:\Users\1000138836\Desktop\12.xlsx',sheet_name='中类小类分析')

for dept_name in dept_xlsx['小类名称'].values.tolist():

result = []

dept = dept_xlsx['小类'].values.tolist()[dept_xlsx['小类名称'].values.tolist().index(dept_name)]

url_dept = quote(dept_name)

for i in range(1,7): # 总共爬取3页,京东商品搜索后先加载半页,向下滚再加载半页。上半页页码为1,下半页页码为2.

url = f"https://search.jd.com/Search?keyword={url_dept}&wq={url_dept}&page={i}"

result = Get_Data_M(url,i)

sheet = Save_Data(result,dept_name,dept)

table = pd.concat([table,sheet])

sleep(random.randrange(5,10))

print(f"已爬取完 {dept_name} 大类")

table.to_excel(r'C:\Users\1000138836\Desktop\result.xlsx',index=None)

if __name__ == '__main__':

Main()

python爬取京东商品价格教科书中文版_Python爬虫,京东商品详情爬取!相关推荐

  1. python爬音乐评论生成词云图_python爬虫+词云图,爬取网易云音乐评论

    又到了清明时节,用python爬取了网易云音乐<清明雨上>的评论,统计词频和绘制词云图,记录过程中遇到一些问题 爬取网易云音乐的评论 一开始是按照常规思路,分析网页ajax的传参情况.看到 ...

  2. python爬数据以字典变量保存_python爬虫第7篇——爬取的数据如何存档

    不能让你暴富,但至少可以奔小康了! 想了解更多精彩内容,快来关注南大盛联 阅读本文,假定你使用的是Windows操作系统下面的python3.X版本. 并且已经掌握了一些基本的python操作.否则, ...

  3. python批量爬取小网格区域坐标系_Python爬虫实例_利用百度地图API批量获取城市所有的POI点...

    上篇关于爬虫的文章,我们讲解了如何运用Python的requests及BeautifuiSoup模块来完成静态网页的爬取,总结过程,网页爬虫本质就两步: 1.设置请求参数(url,headers,co ...

  4. python爬虫beautifulsoup爬当当网_Python爬虫包 BeautifulSoup 递归抓取实例详解_python_脚本之家...

    Python爬虫包 BeautifulSoup  递归抓取实例详解 概要: 爬虫的主要目的就是为了沿着网络抓取需要的内容.它们的本质是一种递归的过程.它们首先需要获得网页的内容,然后分析页面内容并找到 ...

  5. python爬去百度百科词条_Python爬虫入门学习实践——爬取小说

    本学期开始接触python,python是一种面向对象的.解释型的.通用的.开源的脚本编程语言,我觉得python最大的优点就是简单易用,学习起来比较上手,对代码格式的要求没有那么严格,这种风格使得我 ...

  6. python爬取小说基本信息_Python爬虫零基础实例---爬取小说吧小说内容到本地

    Python爬虫实例--爬取百度贴吧小说 写在前面本篇文章是我在简书上写的第一篇技术文章,作为一个理科生,能把仅剩的一点文笔拿出来献丑已是不易,希望大家能在指教我的同时给予我一点点鼓励,谢谢. 一.介 ...

  7. python自动爬取更新电影网站_Python爬虫之—微信实时爬取电影咨询

    本文将介绍如何使用爬虫在微信对话中实现实时的电影咨询爬取功能,希望和大家一起来分享" 1. 撩妹起源 俗话说的好:少壮不撩妹,长大徒伤悲啊! 说的很对,但是在这个撩妹的时代,要想成功把到妹, ...

  8. python爬虫模块排名_Python爬虫使用lxml模块爬取豆瓣读书排行榜并分析

    上次使用了beautifulsoup库爬取电影排行榜,爬取相对来说有点麻烦,爬取的速度也较慢.本次使用的lxml库,我个人是最喜欢的,爬取的语法很简单,爬取速度也快. 本次爬取的豆瓣书籍排行榜的首页地 ...

  9. python从网址爬图片协程_Python爬虫多任务协程爬取虎牙MM图片

    查看: 4420|回复: 241 [作品展示] Python爬虫多任务协程爬取虎牙MM图片 电梯直达 发表于 2019-4-17 21:35:47 | 只看该作者 |倒序浏览 |阅读模式 马上注册,结 ...

最新文章

  1. 提升CUDA程序运行效率的几个关键点
  2. EasyUI下拉框级联
  3. CTR深度学习模型之 DSIN(Deep Session Interest Network) 论文解读
  4. python正则表达式03--字符串中匹配数字
  5. linux的functions之killproc函数详解
  6. 20145339顿珠 MS08_067漏洞测试
  7. python 虚拟环境 mac_Mac下安装Python虚拟环境Virtualenv
  8. SRA 案例:关于华为开发者联盟基础服务文档内容的改进建议(华为开发者联盟文档深度体验官)
  9. PS精讲精练读书笔记
  10. 肯德尔(Kendall)相关系数概述及Python计算例
  11. python点到直线的距离_点到直线距离公式的几种推导
  12. 2023校招美团笔试
  13. 揭露杀毒软件的那些密秘
  14. alert angularjs
  15. Java中扫雷游戏的递归算法_扫雷之递归
  16. 观察力训练(福尔摩斯演绎法)
  17. 调用百度人体分析api 实现人体分析
  18. 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
  19. 前端html利用CSS实现table表格斑马纹隔行换色效果
  20. 【C++】c++ 11中的随机数 ——random

热门文章

  1. React Native 集成 react-native-orientation(横竖屏插件)使用及打包失败问题
  2. python与scratch哪个好_对于儿童来说,Scratch和Python哪个语言更锻炼思维能力
  3. C#开发之Excel录入
  4. 微软应用商店_应用不够网页来凑 微软商店将支持PWA网络应用 尽管这没太大的意义...
  5. Word中批量调整图片大小
  6. 【计算机网络】网络安全知识要点
  7. 51学习-矩阵按键篇
  8. 尚观第15天nagios安装配置
  9. 《天幕红尘》笔记与思考(三)只要条件具足了,结果自然来
  10. Fluke SimpliFiber Pro FTK1475做光纤损耗测试