|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小白一枚,程序可能会有bug,还望鱼油们多多包涵。
现在只是简单写了出来,基本功能实现,但是代码很冗余,还没有使用函数和类进行封装,改进之后会再进行上传。
import requests, os, sys, time
from lxml import etree
if __name__ == '__main__':
if not os.path.exists('./4K图片'):
os.mkdir('4K图片')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'
}
img_type = input('''请选择要下载的类型编号:
1-4K风景 2-4K美女 3-4K动漫
4-4K影视 5-4K明星 6-4K汽车
7-4K动物 8-4K人物 9-4K宗教
10-4K背景
''')
if img_type == '1':
img_type = 'fengjing'
elif img_type == '2':
img_type = 'meinv'
elif img_type == '3':
img_type = 'dongman'
elif img_type == '4':
img_type = 'yingshi'
elif img_type == '5':
img_type = 'mingxing'
elif img_type == '6':
img_type = 'qiche'
elif img_type == '7':
img_type = 'dongwu'
elif img_type == '8':
img_type = 'renwu'
elif img_type == '9':
img_type = 'zongjiao'
elif img_type == '10':
img_type = 'beijing'
else:
print('输入错误,程序即将退出')
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
sys.exit(100)
page = int(input('请输入要下载的页数:'))
for i in range(1, page + 1):
if i == 1:
url = 'https://pic.netbian.com/4k{}/'.format(img_type)
page_text = requests.get(url=url, headers=headers)
page_text.encoding = 'gbk'
page_text = page_text.text
# 创建etree对象,保存页面源码
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for img in li_list:
img_src = img.xpath('./a/img/@src')[0]
img_name = img.xpath('./a/img/@alt')[0]
# 通用处理中文乱码的解决方案
# img_name = img_name.encode('iso-8859-1').decode('gbk')
img_page1_url = 'https://pic.netbian.com' + img_src
img_data = requests.get(url=img_page1_url, headers=headers).content
img_path = './4K图片/' + img_name + '.jpg'
with open(img_path, 'wb') as f:
f.write(img_data)
print(img_name + ' 保存完成!!')
else:
url = 'https://pic.netbian.com/4k{}/index_{}.html'.format(img_type, str(i))
page_text = requests.get(url=url, headers=headers)
page_text.encoding = 'gbk'
page_text = page_text.text
# 创建etree对象,保存页面源码
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for img in li_list:
img_src = img.xpath('./a/img/@src')[0]
img_name = img.xpath('./a/img/@alt')[0]
img_page_url = 'https://pic.netbian.com' + img_src
img_data = requests.get(url=img_page_url, headers=headers).content
img_path = './4K图片/' + img_name + '.jpg'
with open(img_path, 'wb') as f:
f.write(img_data)
print(img_name, ' 保存完成')
|
|