用Python写的图片蜘蛛人代码

代码如下:

#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):
'''下载网页中的图片

@dir 保存到本地的路径
@url 网页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()

(0)

相关推荐

  • python抓取网页图片示例(python爬虫)

    复制代码 代码如下: #-*- encoding: utf-8 -*-'''Created on 2014-4-24 @author: Leon Wong''' import urllib2import urllibimport reimport timeimport osimport uuid #获取二级页面urldef findUrl2(html):    re1 = r'http://tuchong.com/\d+/\d+/|http://\w+(?<!photos).tuchong.co

  • python发送邮件的实例代码(支持html、图片、附件)

    第一段代码: 复制代码 代码如下: #!/usr/bin/python# -*- coding: utf-8 -*- import emailimport mimetypesfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEImage import MIMEImageimport smtplib def sendEmail(authInfo, fromAdd

  • Python实现拼接多张图片的方法

    本文实例讲述了Python实现拼接多张图片的方法.分享给大家供大家参考.具体分析如下:   这里所述计划实现如下操作:   ① 用Latex写原始博文,生成PDF文档; ② 将PDF转成高清的PNG格式的图片; ③ 将多个PNG格式的图片合并成一大张图片; ④ 将最终的大图片直接上传到博文编辑器中 好了,如果将PDF文档转换成其他的图片格式呢?我建议windowns下可用Adobe  Acrobat X Pro软件完成这个工作,操作步骤如下面两图所示.注意在图二中一定要自己指定一个分辨率,不用用

  • Python读取图片属性信息的实现方法

    本文是利用Python脚本读取图片信息,有几个说明如下: 1.没有实现错误处理 2.没有读取所有信息,大概只有 GPS 信息.图片分辨率.图片像素.设备商.拍摄设备等 3.简单修改后应该能实现暴力修改图片的 GPS 信息 4.但对于本身没有 GPS 信息的图片,实现则非常复杂,需要仔细计算每个描述符的偏移量 脚本运行后,读取结果如下 脚本读取的信息 这里和 Windows 属性查看器读到的内容完全一致 图片信息1 图片信息2 源码如下 # -*- coding:utf-8 -*- import

  • Python使用代理抓取网站图片(多线程)

    一.功能说明:1. 多线程方式抓取代理服务器,并多线程验证代理服务器ps 代理服务器是从http://www.cnproxy.com/ (测试只选择了8个页面)抓取2. 抓取一个网站的图片地址,多线程随机取一个代理服务器下载图片二.实现代码 复制代码 代码如下: #!/usr/bin/env python#coding:utf-8 import urllib2import reimport threadingimport timeimport random rawProxyList = []ch

  • python抓取网页图片并放到指定文件夹

    python抓取网站图片并放到指定文件夹 复制代码 代码如下: # -*- coding=utf-8 -*-import urllib2import urllibimport socketimport osimport redef Docment():    print u'把文件存在E:\Python\图(请输入数字或字母)'    h=raw_input()    path=u'E:\Python\图'+str(h)    if not os.path.exists(path):      

  • python使用Image处理图片常用技巧分析

    本文实例讲述了python使用Image处理图片常用技巧.分享给大家供大家参考.具体分析如下: 使用python来处理图片是非常方便的,下面提供一小段python处理图片的代码,需要安装图像处理工具包PIL(Python Image Library). #coding=utf-8 import Image import urllib2 import StringIO import os #改变图片大小 def resize_img(img_path): try: img = Image.open

  • python抓取网页中的图片示例

    复制代码 代码如下: #coding:utf8import reimport urllibdef getHTML(url):    page = urllib.urlopen(url)    html = page.read()    return html def getImg(html,imgType):    reg = r'src="(.*?\.+'+imgType+'!slider)" '    imgre = re.compile(reg)    imgList = re.

  • Python比较两个图片相似度的方法

    本文实例讲述了Python比较两个图片相似度的方法.分享给大家供大家参考.具体分析如下: 这段代码实用pil模块比较两个图片的相似度,根据实际实用,代码虽短但效果不错,还是非常靠谱的,前提是图片要大一些,太小的图片不好比较.附件提供完整测试代码和对比用的图片. 复制代码 代码如下: #!/usr/bin/python # Filename: histsimilar.py # -*- coding: utf-8 -*- import Image def make_regalur_image(img

  • python批量下载图片的三种方法

    有三种方法,一是用微软提供的扩展库win32com来操作IE,二是用selenium的webdriver,三是用python自带的HTMLParser解析.win32com可以获得类似js里面的document对象,但貌似是只读的(文档都没找到).selenium则提供了Chrome,IE,FireFox等的支持,每种浏览器都有execute_script和find_element_by_xx方法,可以方便的执行js脚本(包括修改元素)和读取html里面的元素.不足是selenium只提供对py

随机推荐