Python 实现网络爬虫小程序

Python很简洁,也很强大,作为兴趣,值得一学!

下面这个程序实现的是从一个网站上下载图片,根据自己需要可以进行修改


import re
import urllib

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

def getimg(html):
    reg = r'src="(.*?\.jpg)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    x = 1
    for imgurl in imglist:
        urllib.urlretrieve(imgurl, '%s.jpg' % x)
        x+=1

target = raw_input("Input one url:")
html =  gethtml(target)
print "please wating, pictrues are downloading....."
getimg(html)


posted on 2014-04-30 18:36  wangzhili  阅读(226)  评论(0编辑  收藏  举报