在网上查看大神的关于Python爬虫的文章,代码如下:

#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)

以下则是在运行上述代码过程中遇到的相关问题,以及解决方式,虽然不怎么高级,但是也算是一种学习思路吧。

问题1:在Python3.2的环境下,未运行时,代码会报错:

解决1:将

print getImg(html)

修改为

print (getImg(html))

问题2:代码执行后,报如下错误:

解决2:度娘进行搜索,才发现3.2不兼容2.0的,于是进入官方文档查找最新的调用方式,对这三行进行以下修改,修改前:

import urllib
page = urllib.urlopen(url)
urllib.urlretrieve(imgurl,'%s.jpg' % x)

修改后:

import urllib.request
page = urllib.request.urlopen(url)
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)

问题3:运行代码,提示以下错误:

C:\Python\python.exe D:/selenium/getjpgTest.py
Traceback (most recent call last):
File "D:/selenium/getjpgTest.py", line 20, in <module>
print (getImg(html))
File "D:/selenium/getjpgTest.py", line 13, in getImg
imglist = re.findall(imgre,html)
File "C:\Python\lib\re.py", line 213, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object

Process finished with exit code 1
解决3:百度之后,很容易得到答案,加上下面一句代码即可解决:

html=html.decode('utf-8')

最终得到以下代码:

#coding=utf-8
import urllib.request
import re def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre = re.compile(reg)
html = html.decode('utf-8')
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
x+=1 html = getHtml("http://tieba.baidu.com/p/2460150866")
print (getImg(html))

执行结果如下:

参考文章:1、http://www.cnblogs.com/fnng/p/3576154.html

     2、http://blog.csdn.net/lxh199603/article/details/53192883

Python第一个爬虫学习的更多相关文章

  1. Python 简单网页爬虫学习

    #coding=utf-8 # 参考文章: # 1. python实现简单爬虫功能 # http://www.cnblogs.com/fnng/p/3576154.html # 2. Python 2 ...

  2. python第一个爬虫的例子抓取数据到mysql,实测有数据

    python3.5 先安装库或者扩展 1 requests第三方扩展库 pip3 install requests 2 pymysql pip3 install pymysql 3 lxml pip3 ...

  3. Python爬虫学习第一记 (翻译小助手)

    1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...

  4. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  5. Python项目之我的第一个爬虫----爬取豆瓣图书网,统计图书数量

    今天,花了一个晚上的时间边学边做,搞出了我的第一个爬虫.学习Python有两个月了,期间断断续续,但是始终放弃,今天搞了一个小项目,有种丰收的喜悦.废话不说了,直接附上我的全部代码. # -*- co ...

  6. python爬虫__第一个爬虫程序

    前言 机缘巧合,最近在学习机器学习实战, 本来要用python来做实验和开发环境 得到一个需求,要爬取大众点评中的一些商户信息, 于是开启了我的第一个爬虫的编写,里面有好多心酸,主要是第一次. 我的文 ...

  7. Python爬虫学习:三、爬虫的基本操作流程

    本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...

  8. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  9. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

随机推荐

  1. 数论 + 公式 - HDU 4335 What is N?

    What is N? Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4335 Mean: 给你三个数b.P.M,让你求有多少个n满 ...

  2. 关于json动态拼接响应数据

    在EasyUI http://www.jeasyui.com/demo/main/get_users.php 响应数据如下格式: { "total": "11" ...

  3. 有关Servlet的生命周期说法正确的有( )。

    A.Servlet的生命周期由Servlet实例控制 B.init()方法在创建完Servlet实例后对其进行初始化,传递的参数为实现ServletContext接口的对象 C.service()方法 ...

  4. csdn 模式识别

    http://blog.csdn.net/liyuefeilong/article/details/45217335 模式识别 http://ceit.ucas.ac.cn/index.php?id= ...

  5. UESTC 1511(差分约束)

    题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...

  6. Mybatis框架中Mapper文件传值参数获取。【Mybatis】

    1.参数个数为1个(string或者int) dao层方法为以下两种: /** * 单个int型 */ public List<UserComment> findByDepartmentI ...

  7. Windows游戏编程大师技巧之三角形填充

    一.三角形的种类 三角形一般可以分为如下的四种类型(这四种类型是对于计算机来说的,不是数学意义上的分类): 平顶三角形:就是在计算机中显示的上面两个顶点的Y坐标相同. 平底三角形:就是在计算机中显示的 ...

  8. Mysql 知识点总结

    1.创建数据库    mysqladmin 下面是一个简单的例子,创建名为 yiibai_tutorials1 的数据库. D:\software\mysql--winx64\bin> mysq ...

  9. poj_1860 SPFA

    题目大意 有N种货币,M个兑换点,每个兑换点只能相互兑换两种货币.设兑换点A可以兑换货币C1和C2,给出rate(C1--C2)表示1单位的C1货币可以兑换出的C2货币数目,rate(C2--C1)表 ...

  10. 【BZOJ2095】[Poi2010]Bridges 动态加边网络流

    [BZOJ2095][Poi2010]Bridges Description YYD为了减肥,他来到了瘦海,这是一个巨大的海,海中有n个小岛,小岛之间有m座桥连接,两个小岛之间不会有两座桥,并且从一个 ...