python 批量下載網頁裡的圖片

來源:互聯網
上載者:User

標籤:ade   note   end   取圖   not   linu   head   列表   gre   

python 3.*

import requests
import sys,re
#設定提取圖片url 的Regex
imgre = re.compile(r"<img id=\"imgis\" src=‘(.*?)‘")
#存放找到的 圖片url的列表
all_img_urls = []
#圖片下載後存放位置
save_path = r‘/root‘
#擷取指定網頁中的圖片url
def get_img_url(tmpurl,tmpre,allimgurl,timeout=10):
headers = {‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36‘}
link = requests.get(tmpurl,headers=headers,timeout = timeout)
content = link.text
retsults = tmpre.findall(content)
for i in retsults:
allimgurl.append(i)
#對圖片url進行下載儲存
def save_img(tmpurl,tmppath,timeout=10):
headers = {‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36‘}
ret = requests.get(tmpurl,headers=headers,timeout=timeout)
img = ret.content
filename = ‘{}/{}‘.format(tmppath,tmpurl.split(‘/‘)[-1])
with open(filename,‘wb‘) as f:
f.write(img)

if name == ‘main‘:
urls = [‘http://www.ivsky.com/bizhi/death_note_v17111/pic_{}.html#al_tit‘.format(385791+x) for x in range(10)]
try:
for url in urls:
imgurls = get_img_url(url, imgre, all_img_urls)
except Exception as e:
print(e)

for img in all_img_urls:    try:        save_img(img,save_path)    except Exception as e:        print(e)

python 批量下載網頁裡的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.