资源简介
爬取京东手机销售与评价数据,以excel表格形式存储,以条形图形式展示不同品牌手机在淘宝的评价人数。可以通过更改关键字手机实现对其他商品的爬取。详细介绍https://blog.csdn.net/weixin_42911616/article/details/81506154

代码片段和文件信息
import requests
import pygal
import re
from xlwt import Workbook
import matplotlib.pyplot as plt
import xlrd
import time
from pylab import mpl
from selenium import webdriver
from bs4 import BeautifulSoup
mpl.rcParams[‘font.sans-serif‘] = [‘FangSong‘] # 指定默认字体
mpl.rcParams[‘axes.unicode_minus‘] = False # 解决保存图像是负号‘-‘显示为方块的问题
def key_name( drivernumber ):
#获取页面的内容并返回
name = ‘手机‘
URL_1 = “https://search.jd.com/Search?keyword=“
URL_2 = “&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=“
URL_3=“&cid2=653&cid3=655&page=“
URL_4 = “&s=1&click=0“
URL = ( URL_1 + name + URL_2 + name + URL_3+ str(number)+URL_4)
#driver = webdriver.Firefox()
#driver.implicitly_wait(3)
driver.get(URL)
# 模拟下滑到底部操作
for i in range(1 5):
driver.execute_script(“window.scrollTo(0 document.body.scrollHeight);“)
time.sleep(1)
# 将加载好的页面源码给bs4解析
soup = BeautifulSoup(driver.page_source “html.parser“)
t=0
# 进行信息的抽取(商品名称,价格)
goods_info = soup.select(“.gl-item“)
#driver.close()
return goods_info
def manipulation_data( goods_info sales_count sheet ):
#解析获取的HTML源码,获取数据并对数据进行解析
count=0
for info in goods_info:
title = info.select(“.p-name.p-name-type-2 a“)[0].text.strip()
price_str = info.select(“.p-price“)[0].text.strip()
count_str = info.select(“.p-commit“)[0].text.strip()
#print (title)
price_start=price_str.find(‘¥‘)+1
price_end=price_str.find(‘.‘)
price=int(price_str[price_start:price_end])
#print (price)
if(count_str.find(‘\n‘))>=0:
count_start=count_str.find(‘\n‘)+1
else:
count_start=0
if(count_str.find(‘万+‘))>=0:
count=float(count_str[count_start:count_str.find(‘万‘)])*10000
elif(count_str.find(‘+‘))>=0:
count=int(count_str[count_start:count_str.find(‘+‘)])
#print(count)
sheet.write(sales_count[‘line‘]0title)
sheet.write(sales_count[‘line‘]1int(price))
sheet.write(sales_count[‘line‘]2int(count))
for key in sales_count.keys():
if str(title).find(‘荣耀‘)>=0:
sales_count[‘华为‘]=sales_count[‘华为‘]+(int(count))
elif str(title).find(‘Apple‘)>=0:
sales_count[‘苹果‘]=sales_count[‘苹果‘]+(int(count))
elif str(title).find(str(key))>=0:
sales_count[key]=sales_count[key]+(int(count))
sales_count[‘line‘]= sales_count[‘line‘] + 1
return sales_count
def show(sales_count):
#按照手机销售量进行排序并绘图(网页版)
sales_count=dict(sorted(sales_count.items()key=lambda d:d[1]reverse=True))
print(‘正在绘制直方图‘)
picture=pygal.Bar()
picture.title=“各大品牌手机评价人数直方图 “
picture.x_labels=sales_count.keys()
sales_count.keys()
picture.x_title=“手机品牌“
picture.y_title=“评价人数“
picture.add(‘淘宝‘sales_count.values())
picture.ren
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5101 2018-08-15 13:16 jingdong.py
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论