python简单爬虫
来源:互联网 发布:java调用ie内核 编辑:程序博客网 时间:2024/06/16 11:18
参考博文1),实现了一个简单的爬虫,爬取一个网页上的图片,后面有时间,再学习用beautifulSoup爬取所有网页的图片.
#!usr/bin/env python# -*- coding:utf-8 -*- """@author:cc@file: scraper_test0.py@time:2017/04/02"""# urllib.request.urlopen(url, data=None, [timeout, ]*, ...)# urllib.request.urlretrieve(url[, filename[, reporthook[, data]]])# copy一份url所在文件到本地,并给他取名filename(可加路径)# reprothook传递三条语句:当前已下载了几个blocks: count,每个block大小block_size, 共有total_size个blocks# 通过hook语句追踪下载进度,下载速度,和时间# 正则表达式""". 匹配除换行符外字符b* 匹配b字符0或多次b+ 匹配b字符1或多次b? 匹配b字符0或1次*?/+?/?? 通常*/+/?尽量匹配更多的字符,为了限定匹配到最少,添加?,使之非贪婪匹配b{m} 匹配m个bb{m,n}匹配m~n个b, b{m,}匹配无穷多个b{m,n}? 只匹配m个[abcdefg] 匹配所有a-g中的字符r("www.baidu\.com")只匹配括号里的内容,可在括号外面添加头或尾进行更精确定位\ 反斜杠(总是分别成对解析):python中\\代表一个反斜杠, re中\代表转义字符;例如要用正则化表示一个反斜杠,python中需要"\\\\", re.compiler("\\\\")解析为\或者用原始字符串 r"\\.abc"表示re.compiler(r"\\.abc")解析为\.abc例如采用字符串,先解析字符串在解析正则项re.compile("\.jpg")中解析为.jpg; python对\无法识别re.compile("\\.jpg")中解析为.jpgre.compile("\\\.jpg")中解析为\.jpgre.compile("\\\\.jpg")中解析为\.jpg若采用原始字符解析,直接解析正则项re.compile(r"\.jpg")解析为.jpgre.compile(r"\\.jpg")解析为\.jpgre.compile(r"\\\.jpg")解析为\.jpgre.compile(r"\\\\.jpg")解析为\\.jpg"""import refrom six.moves import urllib# 获取网页内容def getHtml(url): # page = urllib.urlopen(url) #在python3和python2.6等早期版本中已弃用 page = urllib.request.urlopen(url) #等价于urllib2.urlopen() html = page.read() # read(size) return html# 采用正则匹配寻找链接:def getImag(html): # reg = "(https:.*\.jpg)" # reg2 = '"middleURL":"(https:.+?\.jpg)"' # reg = r'src="(.+?\.jpg)" pic_ext' #reg2 = r"src='(.+?\.[a-z]+g)'" # reg2 = r'"[a-zA-Z0-9]+URL":"(https:.+?\.jpg)"' #匹配thunderURL和middleURL:https://******.jpg reg2 = r'(https:.+?\.jpg)' # 在采用match 或者search等之前,需要编译正则表达式为通用的表达类 prog = re.compile(reg2) result = re.findall(prog, html) i = 0 for k in result: if k != '': urllib.request.urlretrieve(k, '/home/cc/temp/%s.jpg' %i) i = i + 1 return resulturl = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=angelababy&oq=ange&rsp=0"# url = "http://tieba.baidu.com/p/2460150866"html = getHtml(url)print(getImag(html))
参考:
1) python实现简单爬虫功能
0 0
- python爬虫入门简单爬虫
- python-简单爬虫
- 简单python爬虫
- Python简单爬虫
- python 简单爬虫
- 简单python爬虫
- Python简单的爬虫
- Python简单爬虫
- python 简单爬虫实现
- python简单文本爬虫
- python超级简单爬虫
- python简单爬虫
- python简单爬虫程序
- python简单爬虫
- Python开发简单爬虫
- Python简单爬虫
- python 简单爬虫原理
- python 3 简单爬虫
- Oracle DG gap 手动同步
- 2017跟着小虎玩着去软考--趣味好玩解析2015年下半年信息系统项目管理师上午试题41-45题
- 时间复杂度O(n)空间复杂度O(1)打印二叉树前中后序
- 进程的管理
- 使用ag-grid的cellRenderer渲染单元格
- python简单爬虫
- DOM节点的删除
- 各种常用排序算法的时间复杂度和空间复杂度
- yuv文件进行下采用和插值
- CAAnimation,iOS动画,核心动画,CAAnimation动画
- 程序设计竞赛校初赛
- 树的同构
- NDK开发第一课:了解AndroidStudio2.2+全新的NDK开发方式及与传统方式开发的区别
- 2017华为软件精英挑战赛解法思路