Python简单爬虫

在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载。

我们可以通过python 来实现这样一个简单的爬虫功能,把我们想要的代码爬取到本地。

实例:

爬取百度贴吧里的图片

import re
import urllib

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

def getImg(html):
    img=r'src="(.*?\.jpg)" pic_ext'
    reimg=re.compile(img)
    imglist=re.findall(reimg,html)
    
    x=0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg'%x)
        x+=1

html=getHtml(r'http://tieba.baidu.com/p/3997245200')
print getImg(html)

爬取完成,打开本地保存目录,就可以看到抓取的图片

posted @ 2015-08-27 11:09  whats  阅读(175)  评论(0编辑  收藏  举报