1. 程式人生 > >Python網路爬蟲-圖片爬取

Python網路爬蟲-圖片爬取

一、匯入urllib.request、re兩個模組

import urllib.request  (用於開啟URL的可擴充套件庫)
import re (用於正則匹配)

二、爬取步驟

1、確定爬取地址
       path="要爬取的網站地址(前面需接http://)"

2、根據地址獲取原始碼
      content=urllib.request.urlopen(path).read().decode("utf-8","ignore")

3、寫正則表示式,匹配相應的資料
      match=re.compile(r'src="(.*?\.jpg)"')

4、儲存
    imagePaths=match.findall(content)

i=0
for imagePath in imagePaths:
    if "http://www.veryedu.cn" not in imagePath:
        imagePath="http://www.veryedu.cn"+imagePath
    i = i + 1
    urllib.request.urlretrieve(imagePath, f"C:\\Users\\Administrator\\Desktop\\圖片\\{i}.jpg")                                                     這裡填的是儲存的地址