编程语言
首页 > 编程语言> > python初入江湖:下载彼岸图网中的4K美女图片

python初入江湖:下载彼岸图网中的4K美女图片

作者:互联网

# https://pic.netbian.com/4kmeinv/下载首面的美女图片

import requests
from lxml import etree

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'}
url = 'https://pic.netbian.com/4kmeinv/'

# 定义存放图表名称和图片页面地址的列表
img_names = []
img_page_urls = []
r = requests.get(url, headers=headers)
r.encoding = 'gbk'
# 解析页面
tree = etree.HTML(r.text)
li_list = tree.xpath('//ul[@class="clearfix"]/li')
for li in li_list:
	# 获取图片页面地址
	img_page_url = 'https://pic.netbian.com' + li.xpath('./a/@href')[0]
	# 获取图片名称
	img_name = li.xpath('./a/b/text()')[0] + '.jpg'
	# 将获取到图片页面址和图片名称存入定义好的列表
	img_page_urls.append(img_page_url)
	img_names.append(img_name)
# 解析图片页面地址,获取图片地址
for i in range(len(img_names)):
	r = requests.get(img_page_urls[i], headers=headers)
	tree = etree.HTML(r.text)
	img_url = 'https://pic.netbian.com' + tree.xpath('//a[@id="img"]/img/@src')[0]
	# 构造下载图片存放地址
	img_path = 'C:/Users/dios/Desktop/4kmeinv/' + img_names[i]
	img_data = requests.get(img_url, headers=headers).content
	with open(img_path, 'wb') as f:
		f.write(img_data)
		print(img_names[i], '下载完成……')

标签:names,img,python,图网,li,初入,headers,url,图片
来源: https://blog.csdn.net/weixin_51707948/article/details/115582950