zoukankan      html  css  js  c++  java
  • 第一个python爬虫——保存淘宝mm图片

    第一次算是成功的爬虫小代码,花了挺长时间的。
    目的:
      获取淘宝mm图片
    现存问题:
      无法获取动态加载的图片,只能得到打开网页后存在的图片
      虽然更换代理仍禁止访问
    收获:
      
     对爬虫的思路相对来说更清晰了——想爬什么,这东西有什么规律,怎么体现在网页上的,如何抓取

      增强了对python基础知识的掌握 1,文件写入的方法 2,json被加载时所需要的条件 3,列表,元组,字典
                      4,网络请求的基本操作 5,基础的应对反爬机制的方法
      
    
    

      


    1
    import json 2 import urllib.response 3 import urllib.request 4 import re 5 import random 6 from json import loads 7 import os 8 9 #请求头数组 10 headerstr = '''Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1 11 Opera/9.80(Macintosh;IntelMacOSX10.6.8;U;en)Presto/2.8.131Version/11.11 12 Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;360SE)''' 13 #获取请求头 14 def headers(): 15 header = headerstr.split(' ') 16 length = len(header) 17 return header[random.randint(0,length-1)] 18 19 #返回基本信息和user_id 20 def getUrlList(): 21 req = urllib.request.Request('https://mm.taobao.com/tstar/search/tstar_model.do?_input_charset=utf-8') 22 req.add_header('User-Agent', headers()) 23 html = urllib.request.urlopen(req).read().decode('gbk') 24 25 #加载json,html解码后才能加载到json中 26 # 注:目前只知道{}内的可以转成json,其余情况未知 27 # 注:[]是列表,()是元组,{}是字典 28 json = loads(html) 29 30 return json['data']['searchDOList'] 31 32 33 #获取mm相册的代号所在的url 34 def getAlbumCode(userId): 35 req = urllib.request.Request('https://mm.taobao.com/self/album/open_album_list.htm?_charset=utf-8&user_id%%20=%s' % userId) 36 req.add_header('User-Agent', headers()) 37 html = urllib.request.urlopen(req).read().decode('gbk') 38 reg = r'class="mm-first" href="//(.*?)"' 39 40 return re.findall(reg, html)[::2] 41 42 43 #获取mm相册的代号 44 def getAlbumInner(code): 45 reg = 'd+' 46 result= re.findall(reg,code) 47 return result[1] 48 49 50 #获取图片地址并保存图片到个人文件夹 51 def getpic(userId,album_id,Album_num,content): 52 #因为下滑可以加载图片,准备将page=1,2,3...的情况做出来,但目前而言由于未知的错误而无法进入page=2的情况,所以暂时搁浅,只选了page=1的情况 53 #这里的page无多少意义,可以忽略 54 page=1 55 56 #index表示下面for循环中在json['picList']的列表中每个元素的指针 57 index = 0 58 59 req = urllib.request.Request('https://mm.taobao.com/album/json/get_album_photo_list.htm?user_id=%s&album_id=%s&page=1'%(userId,album_id)) 60 req.add_header('User-Agent', headers()) 61 html = urllib.request.urlopen(req,timeout=5).read( 62 ).decode('gbk') 63 json = loads(html) 64 65 #json['picList']是一个列表,要通过循环遍历出来每个元素 66 for it in json['picList']: 67 index += 1 68 69 #通过比较url发现大图与小图之间的差距在于290与620,所以直接替换就可以啦 70 picUrl=re.sub('290','620',it['picUrl']) 71 72 #获得的url无法直接写入文件,观察后发现直接加http:就行了 73 pic = 'http:'+picUrl 74 75 #open打开的是将要写入的文件的绝对路径或者说是相对路径 76 contex = urllib.request.urlopen(pic).read() 77 with open(content+"\"+str(Album_num)+'-'+str(page)+'-'+str(index)+".jpg",'wb') as f: 78 79 f.write(contex) 80 81 #先加个代理 82 proxy_support = urllib.request.ProxyHandler({'http': '124.93.87.140:80'}) 83 opener = urllib.request.build_opener(proxy_support) 84 urllib.request.install_opener(opener) 85 86 json = getUrlList() 87 for i in json: 88 userId = i['userId'] 89 realName = i['realName'] 90 city = i['city'] 91 height = i['height'] 92 weight = i['weight'] 93 print (u'发现一位美女,她的名字叫:' +realName, '身高:'+height + 'm','体重:'+ weight + 'kg',u'她现在的居住在-' + city) 94 # 根据mm名字创建文件夹 95 content = 'E:\demo\' + realName 96 if not os.path.exists(content): 97 os.mkdir(content) 98 print('正在为'+realName+'创建文件夹...') 99 #mm的第Album_num个相册 100 Album_num = 0 101 print('正在为你悄咪咪的保存她的图片...') 102 for j in getAlbumCode(userId): 103 code = j 104 album_id=getAlbumInner(code) 105 Album_num+=1 106 getpic(userId, album_id, Album_num,content) 107 print(realName+'的图片已经保存成功啦!')
    只待江流汲海,万木朝东
  • 相关阅读:
    【转】Flask安装
    【转】Mac OS X 中 Zsh 下 PATH 环境变量的正确设置
    【转】谁说Vim不是IDE?(二)
    【转】谁说Vim不是IDE?(三)
    【转】谁说Vim不是IDE?(一)
    【转】终极 Shell
    【转】开始使用Mac OS X——写给Mac新人
    【转】如何实现一个malloc
    《神经网络和深度学习》系列文章十六:反向传播算法代码
    Vue.js之生命周期
  • 原文地址:https://www.cnblogs.com/wanmudong/p/7922492.html
Copyright © 2011-2022 走看看