您的位置:首页 > 编程语言 > Python开发

python实现简单爬虫功能代码

2016-04-27 15:51 1156 查看
实现爬取指定网页,下载图片到本地

原博地址:http://www.cnblogs.com/fnng/p/3576154.html

1.打开网页,读取,最后输出

#coding=utf-8
import urllib

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

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

print html

2.定义getImg函数,获得图片地址。最后输出图片地址
import re
import urllib

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)
return imglist

html = getHtml("http://tieba.baidu.com/p/2460150866")
print getImg(html)

3.增加 下载图片到本地
#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:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1

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

print getImg(html)

学习链接:
正则表达式#http://www.runoob.com/python/python-reg-expressions.html

#http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 爬虫