转载自我自己的博客:http://www.mylonly.com/archives/1401.html

用Python的urllib2库和HTMLParser库写了一个简单的抓图脚本。主要抓的是http://desk.zol.com.cn/meinv/这个链接下的图片,通过得到图集的起始URL地址。得到第一张图片,然后不断的去获取其下一个图片的URL。继而得到全部首页的图集的图片。

整个源代码例如以下。比較简单。写这个仅仅是简单的练手而已

#coding: utf-8 #############################################################
# File Name: girls.py
# Author: mylonly
# mail: mylonly@gmail.com
# Created Time: Mon 09 Jun 2014 09:23:18 PM CST
#########################################################################
#!/usr/bin/python import urllib2,HTMLParser,re #根url
host = "http://desk.zol.com.cn"
#本地保存地址
localSavePath = '/data/girls/'
#起始图片html地址
startHtmlUrl = ''
#图片页Html的地址
htmlUrlList = []
#图片Url地址
imageUrlList = []
#依据得到的图片路径URL将图片下载下来保存本地
def downloadImage(url):
cont = urllib2.urlopen(url).read()
patter = '[0-9]*\.jpg';
match = re.search(patter,url);
if match:
print '正在下载文件:',match.group()
filename = localSavePath+match.group()
f = open(filename,'w+')
f.write(cont)
f.close()
else:
print 'no match' #依据首页得到的图片集遍历每一个图片集
def getImageUrlByHtmlUrl(htmlUrl):
parser = MyHtmlParse(False)
request = urllib2.Request(htmlUrl)
try:
response = urllib2.urlopen(request)
content = response.read()
parser.feed(content)
except urllib2.URLError,e:
print e.reason class MyHtmlParse(HTMLParser.HTMLParser):
def __init__(self,isIndex):
self.isIndex = isIndex;
HTMLParser.HTMLParser.__init__(self)
def handle_starttag(self,tag,attrs):
if(self.isIndex):
if(tag == 'a'):
if(len(attrs) == 4):
if(attrs[0] ==('class','pic')):
newUrl = host+attrs[1][1]
print '找到一处图片的网页链接:',newUrl
global startHtml
startHtmlUrl = newUrl
getImageUrlByHtmlUrl(newUrl)
else:
if(tag == 'img'):
if(attrs[0] == ('id','bigImg')):
imageUrl = attrs[1][1]
print '找到一张图片:',imageUrl
downloadImage(imageUrl)
#imageUrlList.append(imageUrl)
if (tag == 'a'):
if (len(attrs) == 4):
if (attrs[1] == ('class','next')):
nextUrl = host + attrs[2][1]
print '找到一处图片的网页链接:',nextUrl
global startHtmlUrl
if (startHtmlUrl != nextUrl):
getImageUrlByHtmlUrl(nextUrl)
#分析首页得到每一个图片集的链接
indexUrl = 'http://desk.zol.com.cn/meinv/'
m = urllib2.urlopen(indexUrl).read()
parserIndex = MyHtmlParse(True)
parserIndex.feed(m

最新文章

  1. MVC4做网站后台:栏目管理1、添加栏目
  2. C# 显示问题
  3. SQLServer2008R2 error 40解决方法
  4. 无法远程访问虚拟机中的EM (Oracle Enterprise Manager)
  5. position 为absolute时/float 为right,span为block
  6. 我的Python成长之路---第一天---Python基础(3)---2015年12月26日(雾霾)
  7. html浏览器兼容性的 JavaScript语法
  8. Effective C++之‘宁以pass-by-reference-to-const替换pass-by-value’
  9. Silverlight 雷达图和一种特殊泡泡画法
  10. sql之T-SQL
  11. vue基础4-数据绑定
  12. 纪中2018暑假培训day5提高b组改题记录
  13. js拷贝
  14. fiddler学习总结--Web端抓包
  15. python argparse(参数解析)模块学习(二)
  16. CF #442 div2
  17. 【Php】数组遍历,foreach, each, trim()
  18. sourcetree git合并问题
  19. [Javascript] Funciton Expression
  20. oracle decode处理被除数为0 的情况

热门文章

  1. 18/9/22NOIP模拟考
  2. Exclusive or
  3. Service-监听手机来电
  4. Drupal 关于节点(nodes)的理解
  5. thinkphp图片处理
  6. 1.lombok系列1:初识lombok
  7. 4.使用 WSDL 指定的标准 SOAP 消息格式
  8. js的数据类型和typeof数据类型
  9. SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例
  10. win7打不开chm格式文件