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

来源:互联网 发布:app 数据分析工具 编辑:程序博客网 时间:2024/04/28 19:10

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

1.导入的包

# coding:utf-8import urllib2,urllibimport 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 imgUrlurllib.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)

##运行截图
这里写图片描述
##结果截图
这里写图片描述

结束

阅读全文
0 0