简单python 爬虫

在本页面 下载输入的URL 上的图片

 

# -*- coding: utf-8 -*-
import re  # 正则表达式
import urllib                # web页面数据接口


# 输入网址,返回网页源代码
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

#下载网页中图片,默认本文件夹
def getImg(html):
    reg = r'src="(.{1,200}?\.jpg)" pic_ext'        #正则表达式
    
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl, '%s.jpg' % x)    #下载,重命名
        x += 1

html = getHtml("http://tieba.baidu.com/p/4099887318")

print getImg(html)

 

posted @ 2016-02-04 15:43  四点水这  阅读(155)  评论(0编辑  收藏  举报