再来爬取4K美女图片

import requests
import os
from lxml import etree

dirName = "./4kmeimv"
if not os.path.exists(dirName):
os.mkdir(dirName)
url = "http://pic.netbian.com/4kmeinv/index_%d.html"
for page in range(1, 11):
if page == 1:
new_url = "http://pic.netbian.com/4kmeinv/"
else:
new_url = format(url % page)
page_text = requests.get(new_url).text
tree = etree.HTML(page_text)
a_list = tree.xpath('//div[@class="slist"]/ul/li/a')
for a in a_list:
img_src = "http://pic.netbian.com" + a.xpath("./img/@src")[0]
img_name = a.xpath("./b/text()")[0]
img_name = img_name.encode('iso-8859-1').decode('gbk')
img_data = requests.get(img_src).content
imgPath = dirName + '/' + img_name + '.jpg'
with open(imgPath, "wb") as fp:
fp.write(img_data)
print(img_name, "下载成功")

 

原文地址:https://www.cnblogs.com/zhang-da/p/12310986.html