Python的爬虫

下载一个网页的图片:

#-*- coding= utf-8 -*-
import urllib
import re

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 = re.findall(imgre, html)
    x= 0
    for imgurl in imgList:
        #下载的主要语句
        img = urllib.urlretrieve(imgurl, r"D://picture/%s.jpg" %x)
        x = x+1
        print img
#下载页面的地址        
html = getHtml("http://tieba.baidu.com/p/2460150866")
print getImg(html)

#-*- coding=utf-8 -*-
import urllib2
import urllib
import re
import HTMLParser
import time,os

host = "http://desk.zol.com.cn"
startImageUrl =''
localSavePath = 'D:\\picture\\'
ISOTIMEFORMAT='%Y%m%d%H%M%S'

def downloadImage(url):
    imgRe = '[0-9]*\.jpg'
    match = re.search(imgRe, url)
    if match:
        print "Downloading image begin" ,url
        filename = localSavePath + str(time.strftime(ISOTIMEFORMAT))+ r'.jpg'
        img = urllib.urlretrieve(url, filename)
    else:
        print "NO match"   

def getImageUrlByHtmlUrl(htmlUrl):
    parser = MyHtmlParser(False)
    request = urllib2.Request(htmlUrl)
    try:
        response = urllib2.urlopen(request)
        content = response.read()
        parser.feed(content)
    except urllib2.URLError, e:
        print e.reason
        
class MyHtmlParser(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 "Find a image site: ",newUrl
#Question 这句话去掉就会只有一个网页的图片 global 定义的用法
                        startImageUrl = newUrl
                        getImageUrlByHtmlUrl(newUrl)       
                        
                        
        else:
            if(tag == 'img'):
                if(attrs[0] == ('id','bigImg')):
                    imgUrl = attrs[1][1]
                    print " one image : " ,imgUrl
                    downloadImage(imgUrl)
                        
            if(tag == 'a'):
                if(len(attrs) == 4):
                    if(attrs[1] == ('class','next')):
                        nextUrl = host + attrs[2][1]
                        print "Find a next image Link" ,nextUrl
                        global startImageUrl
                        if( nextUrl != startImageUrl ):
                            getImageUrlByHtmlUrl(nextUrl)       
                        
                        
if __name__ == "__main__":
    indexUrl = "http://desk.zol.com.cn/meinv/"        
    page = urllib2.urlopen(indexUrl).read()
    parseIndex = MyHtmlParser(True)   
    parseIndex.feed(page)
    


API: http://blog.csdn.net/tianxicool/article/details/5942523

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值