閱讀608 返回首頁    go 阿裏雲 go 技術社區[雲棲]


python網絡爬蟲抓取圖片

利用python抓取網絡圖片的步驟:

1.根據給定的網址獲取網頁源代碼

2.利用正則表達式把源代碼中的圖片地址過濾出來

3.根據過濾出來的圖片地址下載網絡圖片

import re
import urllib

def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    imglist = imgre.findall(html)
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg' % x)
        x = x + 1        
   
html = getHtml("https://tieba.baidu.com/p/2460150866")
getImg(html)


最後更新:2017-04-03 16:48:56

  上一篇:go python中switch語句用法
  下一篇:go python 可變數據和不可變數據解析