用Python寫的圖片蜘蛛人代碼
更新時(shí)間:2012年08月27日 15:12:34 作者:
寫了個(gè)圖片蜘蛛人玩玩,抓了幾個(gè)網(wǎng)頁(yè)試試,感覺(jué)不不錯(cuò)。核心的代碼可能20行也不到,簡(jiǎn)潔明了,嘻嘻。廢話少說(shuō),翠花,上代碼
復(fù)制代碼 代碼如下:
#coding=utf-8
import os
import sys
import re
import urllib
URL_REG = re.compile(r'(http://[^///]+)', re.I)
IMG_REG = re.compile(r'<img[^>]*?src=([/'"])([^/1]*?)/1', re.I)
def download(dir, url):
'''下載網(wǎng)頁(yè)中的圖片
@dir 保存到本地的路徑
@url 網(wǎng)頁(yè)url
'''
global URL_REG, IMG_REG
m = URL_REG.match(url)
if not m:
print '[Error]Invalid URL: ', url
return
host = m.group(1)
if not os.path.isdir(dir):
os.mkdir(dir)
# 獲取html,提取圖片url
html = urllib.urlopen(url).read()
imgs = [item[1].lower() for item in IMG_REG.findall(html)]
f = lambda path: path if path.startswith('http://') else /
host + path if path.startswith('/') else url + '/' + path
imgs = list(set(map(f, imgs)))
print '[Info]Find %d images.' % len(imgs)
# 下載圖片
for idx, img in enumerate(imgs):
name = img.split('/')[-1]
path = os.path.join(dir, name)
try:
print '[Info]Download(%d): %s'% (idx + 1, img)
urllib.urlretrieve(img, path)
except:
print "[Error]Cant't download(%d): %s" % (idx + 1, img)
def main():
if len(sys.argv) != 3:
print 'Invalid argument count.'
return
dir, url = sys.argv[1:]
download(dir, url)
if __name__ == '__main__':
# download('D://Imgs', 'http://www.163.com')
main()
相關(guān)文章
Python Pandas數(shù)據(jù)分析工具用法實(shí)例
這篇文章主要介紹了Python Pandas數(shù)據(jù)分析工具用法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python logging.info在終端沒(méi)輸出的解決
這篇文章主要介紹了python logging.info在終端沒(méi)輸出的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python for循環(huán)通過(guò)序列索引迭代過(guò)程解析
這篇文章主要介紹了Python for循環(huán)通過(guò)序列索引迭代過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
python數(shù)據(jù)類型之間怎么轉(zhuǎn)換技巧分享
在本篇文章里小編給大家分享的是關(guān)于python數(shù)據(jù)類型之間怎么轉(zhuǎn)換實(shí)例以及小技巧內(nèi)容,有興趣的朋友們參考下。2019-08-08
python利用Opencv實(shí)現(xiàn)人臉識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python利用Opencv實(shí)現(xiàn)人臉識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
python定時(shí)任務(wù)apscheduler的詳細(xì)使用教程
APScheduler的全稱是Advanced?Python?Scheduler,它是一個(gè)輕量級(jí)的?Python定時(shí)任務(wù)調(diào)度框架,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)apscheduler的詳細(xì)使用教程,需要的朋友可以參考下2022-02-02
人工智能學(xué)習(xí)Pytorch教程Tensor基本操作示例詳解
這篇文章主要為大家介紹了人工智能學(xué)習(xí)Pytorch教程Tensor的基本操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
python3去掉string中的標(biāo)點(diǎn)符號(hào)方法
今天小編就為大家分享一篇python3去掉string中的標(biāo)點(diǎn)符號(hào)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

