一、抓取商品id

分析网页源码,发现所有id都是在class=“gl-item”的标签里,可以利用bs4的select方法查找标签,获取id:

获取id后,分析商品页面可知道每个商品页面就是id号不同,可构造url:

将获取的id和构造的url保存在列表里,如下源码:

 def get_product_url(url):
global pid
global links
req = urllib.request.Request(url)
req.add_header("User-Agent",
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36')
req.add_header("GET", url)
content = urllib.request.urlopen(req).read()
soup = bs4.BeautifulSoup(content, "lxml")
product_id = soup.select('.gl-item')
for i in range(len(product_id)):
lin = "https://item.jd.com/" + str(product_id[i].get('data-sku')) + ".html"
# 获取链接
links.append(lin)
# 获取id
pid.append(product_id[i].get('data-sku'))

二、获取商品信息

通过商品页面获取商品的基本信息(商品名,店名,价格等):

         product_url = links[i]
req = urllib.request.Request(product_url)
req.add_header("User-Agent",
'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0')
req.add_header("GET", product_url)
content = urllib.request.urlopen(req).read()
# 获取商品页面源码
soup = bs4.BeautifulSoup(content, "lxml")
# 获取商品名
sku_name = soup.select_one('.sku-name').getText().strip()
# 获取商店名
try:
shop_name = soup.find(clstag="shangpin|keycount|product|dianpuname1").get('title')
except:
shop_name = soup.find(clstag="shangpin|keycount|product|zcdpmc_oversea").get('title')
# 获取商品ID
sku_id = str(pid[i]).ljust(20)
# 获取商品价格

通过抓取评论的json页面获取商品热评、好评率、评论:

获取热评源码:

 def get_product_comment(product_id):
comment_url = 'https://club.jd.com/comment/productPageComments.action?' \
'callback=fetchJSON_comment98vv16496&' \
'productId={}&' \
'score=0&' \
'sortType=6&' \
'page=0&' \
'pageSize=10' \
'&isShadowSku=0'.format(str(product_id))
response = urllib.request.urlopen(comment_url).read().decode('gbk', 'ignore')
response = re.search(r'(?<=fetchJSON_comment98vv16496\().*(?=\);)', response).group(0)
response_json = json.loads(response)
# 获取商品热评
hot_comments = []
hot_comment = response_json['hotCommentTagStatistics']
for h_comment in hot_comment:
hot = str(h_comment['name'])
count = str(h_comment['count'])
hot_comments.append(hot + '(' + count + ')')
return ','.join(hot_comments)

获取好评率源码:

 def get_good_percent(product_id):
comment_url = 'https://club.jd.com/comment/productPageComments.action?' \
'callback=fetchJSON_comment98vv16496&' \
'productId={}&' \
'score=0&' \
'sortType=6&' \
'page=0&' \
'pageSize=10' \
'&isShadowSku=0'.format(str(product_id))
response = requests.get(comment_url).text
response = re.search(r'(?<=fetchJSON_comment98vv16496\().*(?=\);)', response).group(0)
response_json = json.loads(response)
# 获取好评率
percent = response_json['productCommentSummary']['goodRateShow']
percent = str(percent) + '%'
return percent

获取评论源码:

 def get_comment(product_id, page):
global word
comment_url = 'https://club.jd.com/comment/productPageComments.action?' \
'callback=fetchJSON_comment98vv16496&' \
'productId={}&' \
'score=0&' \
'sortType=6&' \
'page={}&' \
'pageSize=10' \
'&isShadowSku=0'.format(str(product_id), str(page))
response = urllib.request.urlopen(comment_url).read().decode('gbk', 'ignore')
response = re.search(r'(?<=fetchJSON_comment98vv16496\().*(?=\);)', response).group(0)
response_json = json.loads(response)
# 写入评论.csv
comment_file = open('{0}\\评论.csv'.format(path), 'a', newline='', encoding='utf-8', errors='ignore')
write = csv.writer(comment_file)
# 获取用户评论
comment_summary = response_json['comments']
for content in comment_summary:
# 评论时间
creation_time = str(content['creationTime'])
# 商品颜色
product_color = str(content['productColor'])
# 商品名称
reference_name = str(content['referenceName'])
# 客户评分
score = str(content['score'])
# 客户评论
content = str(content['content']).strip()
# 记录评论
word.append(content)
write.writerow([product_id, reference_name, product_color, creation_time, score, content])
comment_file.close()

整体获取商品信息源码:

 def get_product_info():
global pid
global links
global word
# 创建评论.csv
comment_file = open('{0}\\评论.csv'.format(path), 'w', newline='')
write = csv.writer(comment_file)
write.writerow(['商品id', '商品', '颜色', '评论时间', '客户评分', '客户评论'])
comment_file.close()
# 创建商品.csv
product_file = open('{0}\\商品.csv'.format(path), 'w', newline='')
product_write = csv.writer(product_file)
product_write.writerow(['商品id', '所属商店', '商品', '价格', '商品好评率', '商品评价'])
product_file.close() for i in range(len(pid)):
print('[*]正在收集数据。。。')
product_url = links[i]
req = urllib.request.Request(product_url)
req.add_header("User-Agent",
'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0')
req.add_header("GET", product_url)
content = urllib.request.urlopen(req).read()
# 获取商品页面源码
soup = bs4.BeautifulSoup(content, "lxml")
# 获取商品名
sku_name = soup.select_one('.sku-name').getText().strip()
# 获取商店名
try:
shop_name = soup.find(clstag="shangpin|keycount|product|dianpuname1").get('title')
except:
shop_name = soup.find(clstag="shangpin|keycount|product|zcdpmc_oversea").get('title')
# 获取商品ID
sku_id = str(pid[i]).ljust(20)
# 获取商品价格
price_url = 'https://p.3.cn/prices/mgets?pduid=1580197051&skuIds=J_{}'.format(pid[i])
response = requests.get(price_url).content
price = json.loads(response)
price = price[0]['p']
# 写入商品.csv
product_file = open('{0}\\商品.csv'.format(path), 'a', newline='', encoding='utf-8', errors='ignore')
product_write = csv.writer(product_file)
product_write.writerow(
[sku_id, shop_name, sku_name, price, get_good_percent(pid[i]), get_product_comment(pid[i])])
product_file.close()
pages = int(get_comment_count(pid[i]))
word = []
try:
for j in range(pages):
get_comment(pid[i], j)
except Exception as e:
print("[!!!]{}商品评论加载失败!".format(pid[i]))
print("[!!!]Error:{}".format(e)) print('[*]第{}件商品{}收集完毕!'.format(i + 1, pid[i])) # 的生成词云
word = " ".join(word)
my_wordcloud = WordCloud(font_path='C:\Windows\Fonts\STZHONGS.TTF', background_color='white').generate(word)
my_wordcloud.to_file("{}.jpg".format(pid[i]))

将商品信息和评论写入表格,生成评论词云:

三、总结

在爬取的过程中遇到最多的问题就是编码问题,获取页面的内容requset到的都是bytes类型的要decode(”gbk”),后来还是存在编码问题,最后找到一些文章说明,在后面加“ignore”可以解决,由于爬取的量太大,会有一些数据丢失,不过数据量够大也不影响对商品分析。

京东口红top 30分析的更多相关文章

  1. Oracle SQL篇(三)Oracle ROWNUM 与TOP N分析

        首先我们来看一下ROWNUM: 含义解释: 1.rownum是oracle为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推.这是一个伪列,可以用于限制查询返回的总行数. 2 ...

  2. Learn golang: Top 30 Go Tutorials for Programmers Of All Levels

    https://stackify.com/learn-go-tutorials/ What is Go Programming Language? Go, developed by Google in ...

  3. 值得收藏!国外最佳互联网安全博客TOP 30

    如果你是网络安全从业人员,其中重要的工作便是了解安全行业的最新资讯以及技术趋势,那么浏览各大安全博客网站或许是信息来源最好的方法之一.最近有国外网站对50多个互联网安全博客做了相关排名,小编整理其中排 ...

  4. Top 30 Nmap Command Examples For Sys/Network Admins

    Nmap is short for Network Mapper. It is an open source security tool for network exploration, securi ...

  5. linux中的调试知识---基础gdb和strace查看系统调用信息,top性能分析,ps进程查看,内存分析工具

    1 调试一般分为两种,可以通过在程序中插入打印语句.有点能够显示程序的动态过程,比较容易的检查出源程序中的有关信息.缺点就是效率比较低了,而且需要输入大量无关的数据. 2 借助相关的调试工具. 3 有 ...

  6. SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析

    最近在研究SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1.jquery ajax跨 ...

  7. HTML JS文字闪烁实现(项目top.htm分析)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- saved from ur ...

  8. 移动开发day4_京东移动页面

    复习 父项身上有哪些属性 可以设置 主轴方向 fd flex-direction : row; column; 主轴子项的排列方式 j justify-content: flex-start;flex ...

  9. 转:XBMC源代码分析

    1:整体结构以及编译方法 XBMC(全称是XBOX Media Center)是一个开源的媒体中心软件.XBMC最初为Xbox而开发,可以运行在Linux.OSX.Windows.Android4.0 ...

随机推荐

  1. 记一次Linux下给硬盘分区格式化操作

    今天找到一张旧TF卡,2G的,正好拿来练习下建立分区 插上orangepi后,fdisk -l看看,可以看到多了一个新的存储设备 /dev/mmcblk1 用fdisk打开它: fdisk /dev/ ...

  2. 从content-type设置看Spring MVC处理header的一个坑

    我们经常需要在HttpResponse中设置一些headers,我们使用Spring MVC框架的时候我们如何给Response设置Header呢? Sooooooooooooo easy, 看下面的 ...

  3. 我的Java设计模式-工厂方法模式

    女朋友dodo闹脾气,气势汹汹的说"我要吃雪糕".笔者心里啊乐滋滋的,一支雪糕就能哄回来,不亦乐乎?! 但是,雪糕买回来了,她竟然说"不想吃雪糕了,突然想吃披萨" ...

  4. Fiddler抓包工具使用详解

    一.Fiddler简介 Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强 ...

  5. maven详解之结构

    maven 父子关系 父项目中打包方式必须是pom  如 <packaging>pom</packaging>,父项目中使用<modules><module& ...

  6. UI自动化测试简介及Selenium工具的介绍和环境搭建

    自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...

  7. 支持向量机(五)SMO算法

    11 SMO优化算法(Sequential minimal optimization) SMO算法由Microsoft Research的John C. Platt在1998年提出,并成为最快的二次规 ...

  8. ajax请求中设置特殊的RequestHeader

    现在ajax应用已经相当广泛了,有很多不错的ajax框架可供使用.ajax是一个异步请求,也主要是一种客户端的脚本行为.那么,如何在请求之前为请求添加特殊的一些头部信息呢? 下面是一个简单的例子,我用 ...

  9. Bootstrap列表与代码样式(附源码)--Bootstrap

    给大家分享下Bootstrap框架中列表与代码样式相关的知识 1.列表 (1)无序列表 <ul> <li>CN217编程</li> </ul> 注意:u ...

  10. 模块:time,random,os,sys

    时间模块 import time # print(time.time()) #时间戳 # print(time.strftime('%Y-%m-%d %X')) #格式化字符 # print(time ...