python爬虫妹子图抓取
目标网址:图片地址
我的github地址:超链接
可以自行感受一下,我就不说了,重点是学习代码,不是图片
#! /usr/bin/python3
# -*- coding: UTF-8 -*-
#name:---XZY---
import requests
from lxml import etree
i = 0
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
"Referer": "https://www.mzitu.com/xinggan/"
}
def data_request():
num = int(input('输入抓取的页面数:'))
for i in range(1, num):
print("==========正在抓取%s页==========" % i)
response = requests.get("https://www.mzitu.com/page/"+ str(i) + "/", headers=headers)
html = etree.HTML(response.content.decode())
xpath_data(html)
def xpath_data(html):
global i
src_1 = html.xpath('//ul[@id="pins"]/li/a/img/@data-original')
alt_1 = html.xpath('//ul[@id="pins"]/li/a/img/@alt')
for src in zip(src_1, alt_1):
file_name = str(i) + ".jpg"
response = requests.get(src, headers=headers)
print("正在抓取图片:" + file_name)
try:
path = str(r'这里写你们想要保存图片的位置\\'+file_name)
with open(path, "wb") as f:
f.write(response.content)
i += 1
except:
print("错误>")
if __name__ == '__main__':
data_request()