python爬虫爬取图片详解_python爬虫(爬取4k图片)+xpath解析

import requests

from lxml import etree

import os

#UA伪装

headers={

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400'

}

#指定url

url = 'http://pic.netbian.com/4kmeinv/'

response = requests.get(url=url,headers=headers)

#乱码处理 手动设置响应数据的编码格式

# response.encoding='utf-8'

page_text =response.text

#实例化对象 数据解析 src属性值 alt属性值

tree = etree.HTML(page_text)

li_list = tree.xpath('//div[@class="slist"]//li')

#创建一个文件夹

if not os.path.exists('./picLib'):

os.mkdir('./picLib')

for li in li_list:

#局部解析

img_src = 'http://pic.netbian.com'+li.xpath([email protected]')[0]

img_title = li.xpath([email protected]')[0]+".jpg"

#通用处理中文乱码的解决方案

img_title = img_title.encode('iso-8859-1').decode('gbk')

print(img_title,img_src)

#请求图片进行持久化存储 图片是二进制数据 用content

img_data = requests.get(url=img_src,headers=headers).content

img_path = 'picLib/'+img_title

with open(img_path,'wb') as fp:

fp.write(img_data)

print(img_title+"下载成功!!!")

你可能感兴趣的:(python爬虫爬取图片详解)