• 技术文章 >后端开发 >Python教程

    python制作花瓣网美女图片爬虫

    2016-06-06 11:14:05原创801

    花瓣图片的加载使用了延迟加载的技术,源代码只能下载20多张图片,修改后基本能下载所有的了,只是速度有点慢,后面再优化下

    import urllib, urllib2, re, sys, os,requests
    path=r"C:\wqa\beautify"
    url = 'http://huaban.com/favorite/beauty'
    #http://huaban.com/explore/zhongwenlogo/?ig1un9tq&max=327773629&limit=20&wfl=1
    i_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"}
    count=0
    
    def urlHandle(url):
      req = urllib2.Request(url, headers=i_headers)
      html = urllib2.urlopen(req).read()
      reg = re.compile(r'"pin_id":(\d+),.+?"file":{"farm":"farm1", "bucket":"hbimg",.+?"key":"(.*?)",.+?"type":"image/(.*?)"', re.S)
      groups = re.findall(reg, html)
      return groups
    
    def imgHandle(groups):
      if groups:
        for att in groups:  
          pin_id = att[0]
          att_url = att[1] + '_fw236'
          img_type = att[2]
          img_url = 'http://img.hb.aicdn.com/' + att_url
    
          r = requests.get(img_url)
          with open(path + att_url + '.' + img_type, 'wb') as fd:
            for chunk in r.iter_content():
              fd.write(chunk)
    
    groups = urlHandle(url)
    imgHandle(groups)
    
    while(groups):
      count+=1
      print count
      pin_id = groups[-1][0]
      print pin_id
      urltemp = url+'/?max=' + str(pin_id) + '&limit=' + str(20) + '&wfl=1'
      print(urltemp)
      groups = urlHandle(urltemp)
      #print groups
      imgHandle(groups)
    

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:python图片爬虫
    上一篇:用Python的Flask框架结合MySQL些一个内存监控程序 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • python中“+=”是什么意思• python字典添加元素的方法是什么• python怎么保留两位小数• python是面向对象还是面向过程• python中怎么合并两个列表
    1/1

    PHP中文网