python3 爬淘女郎
刚学到python 组合数据类型这里,机缘巧合之下得到了一个公开课的视频,也看了前辈写的,取其精华,去其糟粕的爬了一下:
import urllib
from urllib import request
from json import loads
import re
import os
import pymssql
# sql server 数据库
class DB: def __init__(self,host,user,password,database,charset):
self.conn = pymssql.connect(host=host, user=user, password=password, database=database, charset=charset)
self.cur = self.conn.cursor() # 操作
def operateDB(self,sql):
if not self.cur:
print('连接失败')
else:
self.cur.execute(sql)
self.conn.commit() # 关闭
def close(self):
self.cur.close()
self.conn.close()
# 发起请求
def getRequestBody(url):
return urllib.request.urlopen(url).read().decode('gbk')
# 删除字符串中不要的字符
def setStrLegal(body,son):
start_index = body.find(son)
end_index = body.find(son) + len(son)
return body[:start_index] + body[end_index:]
# 获得page页的所有模特信息,封装在字典中
def getShowInfoList(page):
# 页码请求的url
url = 'https://mm.taobao.com/tstar/search/tstar_model.do?_input_charset=utf-8¤tPage={}'.format(page)
body = getRequestBody(url)
dicts = loads(body)
print(dicts['data']['searchDOList'])
return dicts
# 获取模特的信息
def getInfo(userId):
# 每个模特的模特卡的url
url = 'https://mm.taobao.com/self/info/model_info_show.htm?user_id={}'.format(userId)
body = getRequestBody(url).strip()
reg = r'<span>(.*?)</span>'
list_re = re.findall(reg, body)
list_re[1] = setStrLegal(list_re[1],' ').strip()
# 处理错误信息
if len(list_re)-1 >= 8:
list_re.pop()
if len(list_re)-1 < 7:
list_re.append('')
if list_re[4] == '':
list_re[4] = list_re[4].replace('','未知').strip()
if list_re[6] == '':
list_re[6] = list_re[6].replace('', '未知').strip()
if list_re[7] == '':
list_re[7] = list_re[7].replace('', '未知').strip()
list_re[5] = list_re[5].split(' ') reg1 = r'<p>(.*?)</p>'
list_re1 = re.findall(reg1, body)
list_re1[0] = list_re1[0][:list_re1[0].find('CM')]
list_re1[1] = list_re1[1][:list_re1[1].find('KG')]
try:
list_re1[4] = int(list_re1[4][:list_re1[4].find('码')])
except ValueError:
list_re1[4] = 0
return list_re + list_re1
# 一个人所有的相册
def getAlbumList(userId):
# 单击相册时的请求url,请求返回所有相册
url = 'https://mm.taobao.com/self/album/open_album_list.htm?_charset=utf-8&user_id%20={}'.format(userId)
# 获取每一个相册的正侧表达式
body = getRequestBody(url)
reg = r'<a href="(.*?)" target="_blank">'
return re.findall(reg,body)
def getPhotoList(userId,albumId):#url
# 访问相册每个相片
# url = 'https:%s' %url
# print(url)
url = 'https://mm.taobao.com/album/json/get_album_photo_list.htm?user_id={}&album_id={}&top_pic_id=0' \
'&cover=%2F%2Fimg.alicdn.com%2Fimgextra%2Fi1%2F176817195%2FTB1jFcMKFXXXXblXFXXXXXXXXXX_!!0-tstar.jpg' \
'&page=1&_ksTS=1516189198030_166&callback=jsonp167'.format(userId,albumId)
# 获取每一个相册的内容,具体到个体
body = getRequestBody(url)# jsonp数据
data = getData(body,'jsonp167(',')')
dicts = loads(data)
return dicts['picList']
# 取strat_son至end_son之间的字符串
def getData(body,strat_son,end_son):
# strat_son在不在body里面,在返回其位置,否则-1
temp_index = body.find(strat_son)
start_index = temp_index + len(strat_son)
end_index = body.find(end_son, start_index)
return body[start_index:end_index] def savePhoto(photoUrl,path,picId):
file_path = 'f:/mm/' + path
if not os.path.exists(file_path):
os.makedirs(file_path)
auto_down(photoUrl, file_path + '/' + picId + '.jpg') # 递归调用,防止因为文件下载不完全导致的错误
def auto_down(url,file_path):
try:
urllib.request.urlretrieve(url,file_path)
except urllib.request.ContentTooShortError:
print('Network conditions is not good.Reloading.')
auto_down(url,file_path)
# 实施
page = 1
while True:
showInfo_dicts = getShowInfoList(page)
if showInfo_dicts['status'] == 1:
for key in showInfo_dicts['data']['searchDOList']:# getUrlList():30
userId = key['userId']
showInfo_list = getInfo(userId)
print(showInfo_list)
print('正在保存{}的信息'.format(key['realName']))
db = DB('localhost','sa','123456c','Models','utf8')
try:
sql = '''begin tran insert_data
insert into user_basic values('{0}','{1}','{2}','{3}','{4}');
insert into user_extend values('{5}','{6}','{7}','{8}','{9}','{10}');
insert into user_standard values('{11}',{12},{13},'{14}','{15}',{16});
commit tran insert_data'''.format(userId,showInfo_list[0],showInfo_list[2],showInfo_list[1],showInfo_list[4],
userId,showInfo_list[3],showInfo_list[5][0],showInfo_list[5][1],showInfo_list[6],
showInfo_list[7],
userId,float(showInfo_list[8]),float(showInfo_list[9]),showInfo_list[10],
showInfo_list[11],int(showInfo_list[12]))
except ValueError:
continue
try:
db.operateDB(sql)
except pymssql.OperationalError:
continue
print('保存信息完成')
# 获取每个相册url,封在list中
urlList = getAlbumList(userId)
print(urlList)
# 去重复值,相册的url
for album_url in urlList[::7]:
albumId = getData(album_url, 'album_id=', '&')
photoList = getPhotoList(userId, albumId) # 获取封装相片的jsonp数据
print(photoList)
for photo_url in photoList:
photoUrl = ''
if photo_url['picUrl'].find('.jpg_290x10000') != -1:
photoUrl = 'https:' + photo_url['picUrl'].strip('.jpg_290x10000') + '.jpg' # 删除指定字符
else:
photoUrl = 'https:' + photo_url['picUrl']
path = photo_url['userId'] + '/' + photo_url['albumId']
savePhoto(photoUrl, path, photo_url['picId'])
else:
print('该网页已没有想要的数据了,该换了!')
break
page += 1 每个相册只能爬16张。
数据会插入数据中,以下代码,针对得到的数据,做了简单的处理:
import pymssql
from pychartdir import *
def avgMap(data):
# The labels for the bar chart
labels = ["avg_height", "avg_weight", "avg_cup", "avg_shoeSize"] # The colors for the bars
colors = [0xcc0000, 0x66aaee, 0xeebb22, 0xcccccc, 0xcc88ff] # Create a PieChart object of size 600 x 380 pixels.
c = XYChart(600, 380) # Use the white on black palette, which means the default text and line colors are white
c.setColors(whiteOnBlackPalette) # Use a vertical gradient color from blue (0000cc) to deep blue (000044) as background. Use rounded
# corners of 30 pixels radius for the top-left and bottom-right corners.
c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight(), 0x0000cc, 0x000044))
c.setRoundedFrame(0xffffff, 30, 0, 30, 0) # Add a title using 18pt Times New Roman Bold Italic font. Add 6 pixels top and bottom margins to
# the title.
title = c.addTitle("Model average map", "timesbi.ttf", 18)
title.setMargin2(0, 0, 6, 6) # Add a separator line in white color just under the title
c.addLine(20, title.getHeight(), c.getWidth() - 21, title.getHeight(), 0xffffff) # Tentatively set the plotarea at (70, 80) and of 480 x 240 pixels in size. Use transparent border
# and white grid lines
c.setPlotArea(70, 80, 480, 240, -1, -1, Transparent, 0xffffff) # Swap the axis so that the bars are drawn horizontally
c.swapXY() # Add a multi-color bar chart layer using the supplied data. Use bar gradient lighting with the
# light intensity from 0.75 to 2.0
c.addBarLayer3(data, colors).setBorderColor(Transparent, barLighting(0.75, 2.0)) # Set the labels on the x axis.
c.xAxis().setLabels(labels) # Show the same scale on the left and right y-axes
c.syncYAxis() # Set the bottom y-axis title using 10pt Arial Bold font
# c.yAxis().setTitle("USD (millions)", "arialbd.ttf", 10) # Set y-axes to transparent
c.yAxis().setColors(Transparent)
c.yAxis2().setColors(Transparent) # Disable ticks on the x-axis by setting the tick color to transparent
c.xAxis().setTickColor(Transparent) # Set the label styles of all axes to 8pt Arial Bold font
c.xAxis().setLabelStyle("arialbd.ttf", 8)
c.yAxis().setLabelStyle("arialbd.ttf", 8)
c.yAxis2().setLabelStyle("arialbd.ttf", 8) # Adjust the plot area size, such that the bounding box (inclusive of axes) is 30 pixels from the
# left edge, 25 pixels below the title, 50 pixels from the right edge, and 25 pixels from the bottom
# edge.
c.packPlotArea(30, title.getHeight() + 25, c.getWidth() - 50, c.getHeight() - 25) # Output the chart
c.makeChart("avg.png")
conn = pymssql.connect(host='localhost', user='sa', password='123456c', database='Models', charset="utf8")
cur = conn.cursor() cur.execute("""select avg(height),avg(weight),avg(cast(left(cup,2) as int)),avg(shoeSize) from user_standard
where cup not in ('0A' ,'B','M','3X','请选择','不接拍','C','**','QWE'); """)
row = cur.fetchone()
data_avg = []
while row:
data_avg = list(row)
print(data_avg)
row = cur.fetchone()
avgMap(data_avg)
# 关闭
cur.close()
conn.close() 结果图:

python3 爬淘女郎的更多相关文章
- python3爬取网页
爬虫 python3爬取网页资源方式(1.最简单: import'http://www.baidu.com/'print2.通过request import'http://www.baidu.com' ...
- python3爬取女神图片,破解盗链问题
title: python3爬取女神图片,破解盗链问题 date: 2018-04-22 08:26:00 tags: [python3,美女,图片抓取,爬虫, 盗链] comments: true ...
- Python3 爬取微信好友基本信息,并进行数据清洗
Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...
- Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码
题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...
- python3爬取微博评论并存为xlsx
python3爬取微博评论并存为xlsx**由于微博电脑端的网页版页面比较复杂,我们可以访问手机端的微博网站,网址为:https://m.weibo.cn/一.访问微博网站,找到热门推荐链接我们打开微 ...
- python3爬取全民K歌
Python3爬取全民k歌 环境 python3.5 + requests 1.通过歌曲主页链接爬取 首先打开歌曲主页,打开开发者工具(F12). 选择Network,点击播放,会发现有一个请求返回的 ...
- Python3爬取猫眼电影信息
Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...
- python3爬取1024图片
这两年python特别火,火到博客园现在也是隔三差五的出现一些python的文章.各种开源软件.各种爬虫算法纷纷开路,作为互联网行业的IT狗自然看的我也是心痒痒,于是趁着这个雾霾横行的周末瞅了两眼,作 ...
- Python3爬取中文网址
本以为Python3的中文兼容性无比友好,结果还是会有一点问题的. 解决方案如下,就以爬取我自己的一个网页极简翻译为例 #coding:utf8 import urllib.request impor ...
随机推荐
- JDK动态代理[1]----代理模式实现方式的概要介绍
日常工作中经常会接触到代理模式,但一直没有对其进行深究.代理模式一直就像一团迷雾一样存在我心里,什么是代理模式?为什么要使用代理?代理模式有哪些实现?它的底层机制是怎样的?这些问题促使着我迫切想要揭开 ...
- bzoj 4898: [Apio2017]商旅
Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...
- Golang中的坑二
Golang中的坑二 for ...range 最近两周用Golang做项目,编写web服务,两周时间写了大概五千行代码(业务代码加单元测试用例代码).用Go的感觉很爽,编码效率高,运行效率也不错,用 ...
- 对 Java 集合的巧妙利用
我们直接切入正题.首先大致介绍一下 Java 三大集合的一些特征: ①.ArrayList:底层采用数组结构,里面添加的元素有序可以重复. ②.HashSet:底层采用哈希表算法,里面添加的元素无序不 ...
- requireJS对文件合并与压缩(二)
requireJS对文件合并与压缩 RequireJS提供了一个打包与压缩工具r.js,r.js的压缩工具使用UglifyJS进行压缩的或Closure Compiler.r.js下载 require ...
- Linux下Jdk的安装和jdk环境变量的设置
我们在Linux下安装系统软件的时候,经常遇到一些系统环境变量配置的问题.什么是环境变量?如何定制环境变量?我将在下面做一些介绍.一.什么是环境变量?Linux是一个多用户的操作系统.多用户意味着每个 ...
- APP网络测试要点和弱网模拟
当前APP网络环境比较复杂,网络制式有2G.3G.4G网络,还有越来越多的公共Wi-Fi.不同的网络环境和网络制式的差异,都会对用户使用app造成一定影响.另外,当前app使用场景多变,如进地铁.上公 ...
- HTTP状态码、请求方法、响应头信息
HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应 ...
- CRUL学习记录
在学习web相关知识的时候,了解到crul可以对网址进行请求,并可以看到网站的响应. 1.curl获取web网站返回的网页内容 命令:curl http://XXXXX 2.curl查看web网站的s ...
- 多行文字水平垂直居中在div
<BODY> <div class="box"> <h3>1.单行文字居中</h3> <!--设置行高来实现- ...