#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
运行结果如下