用python下载xxxx网站封面作品的所有图片

实现思路:
  1.识别出封面作品集,
  2.点击打开作品集
  3.当作品集下载完成后,下载图片
技术实现:
  1.先研究和分析封面作品集的html元素,判断出集品集是放在那个元素下 [这个是重点]

    1.1 经过研究发现封面作品的连接格式如下: 

      <div class="img-box">

      <a href="http://photo.xxxx.cn/lastphoto-htx-id-5570754-p-0.xhtml" target="_blank" title="百般红紫斗芳菲">
    1.2 打开作品集后,发现jpg图片都是存储在如下格式下:
      <img title="梦回_4" data-photo-item-id="223088169" item="223088169" role="playLivephoto" src="http://www.xxxx.cn/css_common/v3/images/img_loading.gif" data-lazyload-img-src="http://image18-c.xxxx.cn/myxxxx/myphoto/20170402/23/5439720170402231558067_640.jpg?900x1350_120" data_org_bimg ="http://image18-c.xxxx.cn/myxxxx/myphoto/20170402/23/5439720170402231558067.jpg?900x1350_120" data_img_small="http://image18-c.xxxx.cn/myxxxx/myphoto/20170402/23/5439720170402231558067_640.jpg?900x1350_120" class="photo-item" οnlοad="if(Sys.ie6)resize_img(this,994,100000)"/>

2.用urllib2打开封面有作品页,并把结果返回给我BeautifulSoup进行格式化。
     html=urllib2.urlopen(url) 返回url的格式集

2.通过beautifulsoup读取这些元素的值

      html_format=BeautifulSoup(html.read(),'html5lib')
      tagset=html_format.find_all("div") ;返回找到的所有tag的结果集
      访问方法:
      for tag in tagset:
        tag["tag属性"][-1]  返回tag属性值
4.发现作品图片,并下载另存为
     urllib.urlretrieve(url,save path)

5.通过以上方法的组合,我们批量下载到你想要的图片

具体代码实现:

import urllib2
import urllib
from bs4 import BeautifulSoup


def get_url(input_url):
    url_set=[]
    url=urllib2.urlopen(input_url)
    html=BeautifulSoup(url.read(),'html5lib')
    div=html.find_all('div')
    for i in div:
       try:
            #print(i["class"][0])
            if i["class"][0]=="img-box":
                #print (i.a["href"])
                url_set.append(i.a["href"])
       except Exception as e:
           pass
    return url_set


def open_url(urlset,save_path):
    file_no=0
    img_path=""
    for i in urlset:
        html=urllib2.urlopen(i)
        print("opening")
        print(i)
        html_format=BeautifulSoup(html.read(),"html5lib")
        imgset=html_format.find_all("img")       
        for img in imgset:
            try:
                file_no=file_no+1             
                img_path=img["data_org_bimg"][0:-1]                                
                file_name=img_path[img_path.find(".jpg")-5:img_path.find(".jpg")+4]               
                file_path=save_path+file_name
                print(file_path)
                try:
                   urllib.urlretrieve(img_path,file_path)
                except Exception as e:
                    print(e)
            except Exception as e:
                pass
               


if __name__=='__main__':
    urlset=get_url("http://photo.xxxx.cn/vision.htx&p=1&index_type=hot&tid=-1&gid=0#list")
    open_url(urlset,"d:\xxxx\model\\")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值