使用requests下载彼岸图W高清图片到本地
requests_pic_netbian_com.py
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 # File:requests_pic_netbian_com.py # Author:LGSP_Harold import requests from lxml import etree headers = { 'authority': 'pic.netbian.com', 'method': 'GET', 'path': '/4kmeinv/', 'scheme': 'https', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'cache-control': 'no-cache', 'dnt': '1', 'pragma': 'no-cache', 'referer': 'https://pic.netbian.com/4kqiche/', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', } def down_img(page_obj): items = page_obj.xpath('//div[@class="slist"]//li/a/@href') for item in items: detail_url = 'https://pic.netbian.com' + item detail_response = requests.get(url=detail_url, headers=headers) detail_response.encoding = 'gbk' detail_obj = etree.HTML(detail_response.text) detail_title = detail_obj.xpath('//div[@class="photo-hd"]/h1/text()')[0] file_path = './imgs/netbian_com/' + detail_title + '.jpg' img_src = 'https://pic.netbian.com' + \ detail_obj.xpath('//div[@class="photo-pic"]/a[@id="img"]/img/@src')[0] img_response = requests.get(url=img_src) if img_response.status_code == 522: print('%s图片不存在' % detail_title) continue with open(file_path, 'wb') as f: f.write(img_response.content) print('%s图片成功下载' % detail_title) for page in range(1, 9): if page == 1: url = 'https://pic.netbian.com/4kmeinv/' else: url = 'https://pic.netbian.com/4kmeinv' + '/index_' + str(page) + '.html' response = requests.get(url=url, headers=headers) page_obj = etree.HTML(response.text) down_img(page_obj)
略懂,略懂....