python 学习之爬虫练习
通过学习python,写两个简单的爬虫,没用线程,本地抓取速度还不错,有些瑕疵就是抓的图片有些显示不出来,代码做个笔记记录下:
# -*- coding:utf-8 -*- import re
import urllib.request
import os url = "http://www.58pic.com/yuanchuang/0/day-" def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read().decode('gbk')
return html def getImg(html,num):
reg = r'src="(.*?)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
os.mkdir(r"G:\collect/%d" % num)
filePath = r"G:\collect/%d/" % num
for imgurl in imglist:
f=open(filePath+str(x)+".jpg",'wb')
req=urllib.request.urlopen(imgurl)
buf=req.read()
f.write(buf)
x+=1 for i in range(1,10):
getUrl = url+"%d.html" % i
print(getUrl)
html = getHtml(getUrl)
#print(html)
print(getImg(html,i))
最终的结果如下图:


根据上面的初步代码,优化后加强版的爬虫代码,对于链接的状态异常的抛出异常后在继续执行程序。代码如下:
# -*- coding:utf-8 -*- import re
import urllib.request
import os url = "http://www.58pic.com/psd/" def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read().decode('gbk')
return html def getImg(html,num):
reg = r'src="(.+?\.jpg)" class="show-area-pic" id="show-area-pic" alt="(.*?)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
print(imglist)
filePath = r"F:\Py/collect/%d/" % num
isCreate = os.path.exists(filePath)
if isCreate == False :
os.mkdir(r"F:\Py/collect/%d" % num)
for img in imglist:
title = img[1]
f=open(filePath+title+".jpg",'wb')
req=urllib.request.urlopen(img[0])
buf=req.read()
f.write(buf) for i in range(22797263,22797666):
getUrl = url+"%d.html" % i
#status = urllib.request.urlopen(getUrl).code
try:
html = getHtml(getUrl)
#print(html)
getImg(html,i)
except urllib.request.URLError as e:
print(e.code)
print(e.reason)
python 学习之爬虫练习的更多相关文章
- Python学习网络爬虫--转
原文地址:https://github.com/lining0806/PythonSpiderNotes Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 另外,比较常用的爬虫框架Scra ...
- python学习之爬虫(一) ——————爬取网易云歌词
接触python也有一段时间了,一提到python,可能大部分pythoner都会想到爬虫,没错,今天我们的话题就是爬虫!作为一个小学生,关于爬虫其实本人也只是略懂,怀着"Done is b ...
- 【Python学习】爬虫报错处理bs4.FeatureNotFound
[BUG回顾] 在学习Python爬虫时,运Pycharm中的文件出现了这样的报错: bs4.FeatureNotFound: Couldn’t find a tree builder with th ...
- python学习之爬虫初体验
作业来源: "https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2851" ** 1.简述爬虫原理 通用爬虫 即(搜索 ...
- python学习笔记——爬虫学习中的重要库urllib
1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...
- python学习笔记——爬虫中提取网页中的信息
1 数据类型 网页中的数据类型可分为结构化数据.半结构化数据.非结构化数据三种 1.1 结构化数据 常见的是MySQL,表现为二维形式的数据 1.2 半结构化数据 是结构化数据的一种形式,并不符合关系 ...
- Python学习---网页爬虫[下载图片]
爬虫学习--下载图片 1.主要用到了urllib和re库 2.利用urllib.urlopen()函数获得页面源代码 3.利用正则匹配图片类型,当然正则越准确,下载的越多 4.利用urllib.url ...
- Python学习 之 爬虫
目标:下载贴吧或空间中所有图片 步骤:(1)获取页面代码 (2)获取图片URL,下载图片 代码如下: #!/usr/bin/python import re import urllib def get ...
- python学习笔记——爬虫的抓取策略
1 深度优先算法 2 广度/宽度优先策略 3 完全二叉树遍历结果 深度优先遍历的结果:[1, 3, 5, 7, 9, 4, 12, 11, 2, 6, 14, 13, 8, 10] 广度优先遍历的结果 ...
随机推荐
- Repeater控件的详细用法
中隔行(交替项)呈现一次.通过设置 AlternatingItemTemplate 元素的样式属性,可以为其指定不同的外观. FooterTemplate在所有数据绑定行呈现之后呈现一次的元素.典型的 ...
- jQuery_easyUI 合并单元格 (DataGrid 数据表格)
<table id="dg" style="height:350px;z-index:-5555; " class="easyui-datagr ...
- pca图像识别
代码下载:基于PCA(主成分分析)的人脸识别 人脸识别是一个有监督学习过程,首先利用训练集构造一个人脸模型,然后将测试集与训练集进行匹配,找到与之对应的训练集头像.最容易的方式是直接利用欧式距离计算测 ...
- C++服务器设计(二):应用层I/O缓冲
数据完整性讨论 我们已经选择了I/O复用模型作为系统底层I/O模型.但是我们并没有具体解决读写问题,即在我们的Reactor模式中,我们怎么进行读写操作,才能保证对于每个连接的发送或接收的数据是完整的 ...
- (原)ubuntu14手动安装matplotlib1.5
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5681059.html 参考网址: http://matplotlib.org/users/instal ...
- 修改mysql编码为UTF-8
mysql> show variables like '%character%'; +--------------------------+--------------------------- ...
- activiti笔记三 Activiti问题重现
测试的时候出现一个异常: ContextLoader.initWebApplicationContext(308) | Context initialization failed org.spring ...
- CSS3动画之旋转魔方盒
步骤: 1.大盒子里盛放六个子盒子代表立方体的6个面: 2.子盒子开启3d效果 transform-style:preserve-3d; 3.上下面沿X轴旋转90度,一个上移盒子一半高,一个下移盒子 ...
- HDU 3501 Calculation 2(欧拉函数)
Calculation 2 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- connectionStrings基本配置
常用connectionStrings配置: <connectionStrings> <add name="LocalSqlServer" ...