python如何使用request爬取图片

下面是代码的简单实现,变量名和方法都是跑起来就行,没有整理,有需要的可以自己整理下:

image2local:

import requests
import time
from lxml import etree
import os #存储位置 dir = 'xxxxxx' #网址地址 image_host = 'https://www.27270.com' #获取爬取列表 def get_list(page_detail=''): #爬取列表 page = requests.get('https://www.27270.com/ent/meinvtupian/list_11_{0}.html'.format(page_detail)) #解析列表数据 image_urls = etree.HTML(page.text) print(image_urls.xpath('/html/body/div[2]/div[7]/ul/li/a[2]/@href')) pages = image_urls.xpath('/html/body/div[2]/div[7]/ul/li/a[2]/@href') return pages def getEntityUrl(url): #爬取传过来的地址 page = requests.get(image_host+url) page.keep_alive = False image_urls = etree.HTML(page.content) try: image = image_urls.xpath('//*[@id="picBody"]/p/a[1]/img/@src')[0] next = image_urls.xpath('//*[@id="nl"]/a/@href')[0] title = image_urls.xpath('/html/body/div[2]/div[2]/h1/text()')[0] except: return False #爬取第一张 if next.find('##')>1: return False else: result = image2local(image,title,next) return True #爬取图片 def image2local(url,title,name): title = title.split('(')[0] if not os.path.exists(dir + title): os.mkdir(dir + title) try: image = requests.get(url) except: return True # print(image.content+) with open(dir + title+'/{0}.jpg'.format(name),'wb') as f: f.write(image.content) f.close() return True if __name__ == '__main__': x = range(1,215) for i in x: list = get_list(i) for image in list: num = 1 result = True while result: next = image.replace('.','_{0}'.format(num)+'.') num = num+1 result = getEntityUrl(next) 

python代码是现学现写的,大家勿喷

猜你喜欢

转载自www.cnblogs.com/jack-jt-z/p/10474208.html