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

Python第一个爬虫(爬取贴吧图片)

2017-09-28 14:11 330 查看

Python第一个爬虫(爬取贴吧图片)

1.导入的包

# coding:utf-8
import urllib2,urllib
import re


2.使用urllib2打开页面读取数据

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


3.使用正则表达式,解析数据,获取想要的数据

def getImg(html):
reg = r'murl":"(.+?)"'
imgre = re.compile(reg)
imgList = re.findall(imgre,html)

x=0;
for imgUrl in imgList:
print imgUrl
urllib.urlretrieve(imgUrl,'C:\Users\xxx\Desktop\ss\%s.jpg'%x)//调用方法下载图片
x+=1


4.调用方法,爬取贴吧的图片

html = getHtml("http://tieba.baidu.com/photo/g/bw/picture/list?kw=%E5%9B%BE%E7%89%87&alt=jview&rn=200&tid=1433290786&pn=1&ps=1&pe=40&info=1&_=1506576344661&red_tag=b1600243292")
getImg(html)


##运行截图



##结果截图



结束

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 爬虫