爬取彼岸图网图片
作者:互联网
#爬取第一页图片 import os import requests from lxml import etree dirName = 'GirLslib' #创建文件夹 if not os.path.exists(dirName): #如果文件夹Girlslib不存在,就创建。 os.mkdir(dirName) headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53' } main_url = 'https://pic.netbian.com/4kmeinv/' reponse = requests.get(url=main_url,headers=headers) reponse.encoding = 'gbk' #reponse.encoding的数据类型为str page_text = reponse.text tree = etree.HTML(page_text) li_list = tree.xpath('//div[@class="slist"]/ul/li') for li in li_list: title = li.xpath('./a/img/@alt')[0]+'.jpg' #图片名称 img_scr = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0] #图片位备定位 img_data = requests.get(url=img_scr,headers=headers).content #图片数据 imgPath = dirName + '/' + title with open(imgPath,'wb') as fp: fp.write(img_data) print(title,'保存成功!')
# _author: Admin # date: 2021/11/17 #爬取多页图片 import os import requests from lxml import etree dirName = 'GirLslib' #创建文件夹 if not os.path.exists(dirName): #如果文件夹Girlslib不存在,就创建。 os.mkdir(dirName) headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53' } url = 'https://pic.netbian.com/4kmeinv/index_%d.html' #定义一个通用的url模板:不可变 for page in range(1,6): if page == 1: new_url = 'https://pic.netbian.com/4kmeinv/' #由于第一页特殊,所以单独判断 else: new_url = format(url%page) reponse = requests.get(url=new_url,headers=headers) reponse.encoding = 'gbk' #reponse.encoding的数据类型为str page_text = reponse.text tree = etree.HTML(page_text) li_list = tree.xpath('//div[@class="slist"]/ul/li') for li in li_list: title = li.xpath('./a/img/@alt')[0]+'.jpg' #图片名称 img_scr = 'http://pic.netbian.com'+li.xpath('./a/img/@src')[0] #图片位备定位 img_data = requests.get(url=img_scr,headers=headers).content #图片数据 imgPath = dirName + '/' + title with open(imgPath,'wb') as fp: fp.write(img_data) print(title,'保存成功!')
标签:img,url,图网,reponse,彼岸,li,爬取,headers,dirName 来源: https://www.cnblogs.com/longfei825/p/15566339.html