使用Python的urllib和urllib2模块制作爬虫的实例教程
urllib
学习python完基础,有些迷茫.眼睛一闭,一种空白的窒息源源不断而来.还是缺少练习,遂拿爬虫来练练手.学习完斯巴达python爬虫课程后,将心得整理如下,供后续翻看.整篇笔记主要分以下几个部分:
- 1.做一个简单的爬虫程序
- 2.小试牛刀--抓取百度贴吧图片
- 3.总结
1.做一个简单的爬虫程序
首先环境描述
- Device: Mba 2012 Yosemite 10.10.1
- Python: python 2.7.9
- 编辑器: Sublime Text 3
这个没有什么好说的,直接上代码吧!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
很简单,基本上没有可说的,这个也就是python的魅力,几行代码就完成.
当然我们仅仅抓取网页,没有实在的价值.接下来我们就开始做一点有意义的事情.
2.小试牛刀
抓取百度贴吧图片
其实也很简单,因为要抓取图片,还需要先分析一下网页源代码
(这里以知道基本html知识,浏览器以chrome为例)
如图,这里简要说下步骤,请参考.
打开网页,右键点击,选择"inspect Element"(最下面这一项)
点击下面弹起来的框框最左边那个问号,问号会变成蓝色
移动鼠标去点击我们想要抓取的图片(一个萌妹子)
如图,我们就可以图片在源码中的位置了
下面将源码相关拷贝出来
1 2 |
|
经分析和对比(这里略掉),基本上可以看到要抓取的图片几个特征:
- 在img标签下
- 在名为BDE_Image的类下面
- 图片格式为jpg
正则表达式后续我会更新,请关注
依照上述判断,直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
如图,我们就抓取你懂的图片
3.总结
如上两节,我们就很轻松的就可以网页或者图片.
补充一点小技巧,如果遇到不是很明白的库或者方法,可以通过以下方法进行初步了解.
- dir(urllib) #查看当前库有哪些方法
- help(urllib.urlretrieve) #查看跟当前方法相关的作用或者参数,官方比较权威
或者https://docs.python.org/2/library/index.html进项相关搜索.
当然百度也可以,但是效率太低.建议使用 http://xie.lu 进行相关搜索(你懂了,绝对满意).
这里我们讲解如何抓取网页和下载图片,在下面我们会讲解如何抓取有限制抓取的网站.
urllib2
上面我们讲解如何抓取网页和下载图片,在下一节里面我们会讲解如何抓取有限制抓取的网站
首先,我们依然用我们上一节课的方法去抓取一个大家都用来举例的网站
- 1.抓取受限网页
- 2.对代码进行一些优化
1.抓取受限网页
首先使用我们上一节学到的知识测试一下:
1 2 3 4 5 6 7 8 9 10 11 |
|
1 |
|
此处我们的输出为403,代表拒绝访问;同理200表示请求成功完成;404表示网址未找到.
可见csdn已做了相关屏蔽,通过第一节的方法是无法获取网页,在这里我们需要启动一个新的库:urllib2
但是我们也看到浏览器可以发那个文,是不是我们模拟浏览器操作,就可以获取网页信息.
老办法,我们先来看看浏览器是如何提交请求给csdn服务器的.首先简述一下方法:
- 打开网页,右键点击,选择"inspect Element"(最下面这一项)
- 点击下面弹起来的框框的Network选项卡
- 刷新网页,就可以看到Network选项卡抓取了很多信息
- 找到其中一个信息展开,就能看到请求包的Header
以下就是整理后的Header信息
1 2 3 4 |
|
然后根据提取的Header信息,利用urllib2的Request方法模拟浏览器向服务器提交请求,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
呵呵,你限制我,我就跳过你的限制.据说只要浏览器能够访问的,就能够通过爬虫抓取.
2.对代码进行一些优化
简化提交Header方法
发现每次写那么多req.add_header对自己来说是一种折磨,有没有什么方法可以只要复制过来就使用.答案是肯定的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
发现是不是很简单,在这里感谢斯巴达的无私赐教.
提供动态头部信息
如果按照上述方法进行抓取,很多时候会因为提交信息过于单一,被服务器认为是机器爬虫进行拒绝.
那我们是不是有一些更为智能的方法提交一些动态的数据,答案肯定也是肯定的.而且很简单,直接上代码!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
其实很简单,这样我们就完成了对代码的一些优化.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...
