Python下载"校花网"的所有套图 按照一定规律进行文件夹分类存储

#coding = utf-8
from urllib.request import urlopen,Request
import gzip
from urllib.request import urlretrieve
import os
map = ["大学校花","高中校花","专科校花","大学美女","美女校花","校园美女","清纯美女"]
print("欢迎来到校花网观赏")
print("欣赏类别有:\n1,大学校花\n2,高中校花\n3,专科校花\n4,大学美女\n5,美女校花\n6,校园美女\n7,清纯美女")
headerds = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}
num = 0
def create_dir(name):
    if not os.path.exists(name):
        os.makedirs(name)

def come_on (tag0,num0):
    if   (tag0 == 0):
        path0 = "http://www.521609.com/daxuexiaohua/list3%d.html" % (num0)    
    elif (tag0 == 1):
        path0 = "http://www.521609.com/gaozhongxiaohua/list5%d.html" % (num0)   
    elif (tag0 == 2):
        path0 = "http://www.521609.com/zhuankexiaohua/list7%d.html" % (num0)    
    elif (tag0 == 3):
        path0 = "http://www.521609.com/daxuemeinv/list8%d.html" % (num0)        
    elif (tag0 == 4):
        path0 = "http://www.521609.com/meinvxiaohua/list12%d.html" % (num0)     
    elif (tag0 == 5):
        path0 = "http://www.521609.com/xiaoyuanmeinv/list_%d.html" % (num0)     
    elif (tag0 == 6):
        path0 = "http://www.521609.com/qingchunmeinv/list26%d.html" % (num0)    
    return path0


def getImg(path,headerds):
    req = Request(url=path, headers=headerds)
    conn = urlopen(req)
    if conn.code == 200:
        data = conn.read()
        data = data.decode(encoding="gbk")
#        print(data)
        html = etree.HTML(data)
        imgs = html.xpath("//div[@id='content']/div[@class='left']/div[@class='index_img list_center']/ul/li/a/img")
        for img in imgs:
            src = img.xpath("./@src")
            alt = img.xpath("./@alt")
            if not os.path.exists("Picture/%s/第%s页"%(map[tag],num)):
                os.makedirs("Picture/%s/第%s页"%(map[tag],num))
            urlretrieve("http://www.521609.com%s" % (src[0]), "Picture/%s/第%s页/%s.jpg" %(map[tag],num,alt[0]))
    print("下载完成,请浏览")

create_dir('Picture')
a = 0
while a < 20:

    tag = input("请输入您要欣赏的类别:")
    tag = int(tag) - 1
    num = input("输入您想下载的页数:")
    num = int(num)

    path = come_on(tag0=tag,num0=num)
    getImg(path, headerds)

    exit = input("是否结束下载(Y/N):")
    if (exit == "Y"):
        break
    a += 1
    if a == 100:
        break


运行结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值