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代碼是現學現寫的,大家勿噴


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM