1. 程式人生 > 實用技巧 >使用Python爬取網頁圖片

使用Python爬取網頁圖片

下載https://www.mayiwenku.com/p-4957235.html

網頁的MATLAB答案

下載一張照片

import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
url = "https://www.mayiwenku.com/FileRoot1/2018-6/30/ad6c8eca-52e2-4593-ad2e-28676ef6f998/ad6c8eca-52e2-4593-ad2e-28676ef6f9981.gif"
 
#傳送get請求
response = requests.get(url,headers = headers)
 
#變為byte流資料
content = response.content
 
#儲存圖片
with open('C:\\Users\\Administrator\\Desktop\\答案.jpg','wb') as f:
    f.write(content)
    f.close()

  

下載多張照片

import requests

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}

for i in range(1,34):
	url="https://www.mayiwenku.com/FileRoot1/2018-6/30/ad6c8eca-52e2-4593-ad2e-28676ef6f998/ad6c8eca-52e2-4593-ad2e-28676ef6f998{}.gif".format(i)

	#傳送get請求
	response = requests.get(url,headers = headers)
 
	#變為byte流資料
	content = response.content
	 
 
	#儲存圖片
	f=open('C:\\Users\\Administrator\\Desktop\\MATLAB答案\\答案{}.jpg'.format(i),'wb')
	f.write(content)
	f.close()