Python简单爬虫
来源:互联网 发布:剑灵捏脸超萌萝莉数据 编辑:程序博客网 时间:2024/06/08 08:03
一、什么是爬虫
爬虫:一段自动抓取互联网信息的程序。
价值:互联网数据为我所用。
二、爬虫架构
运行流程:
三、几种实现方式
四、URL管理器
定义:管理待抓取URL集合和已抓取URL集合;
防止重复抓取、防止循环抓取
五、网页下载器
1)定义:将互联网上URL对应的网页下载到本地的工具。
2)几种网页下载器:
urllib2——Python官方基础模块
requests——第三方包更强大
a)urllib2下载网页方法:最简洁方法
b)urllib2下载网页方法2:添加data、http header
c)urllib2下载网页方法3:添加特殊情景的处理器
`
#-- coding: UTF-8 --
import urllib2
import cookielib
url = “http://www.baidu.com”
print ‘第一种方法’
response1 = urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())
print ‘第二种方法’
request = urllib2.Request(url)
request.add_header(“user-agent”,”Mozilla/5.0”)
response2 = urllib2.urlopen(url)
print response2.getcode()
print len(response2.read())
print ‘第三种方法’
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()`
六、网页解析器
1)定义:从网页中提取有价值数据的工具。
2)几种网页解析器:
3)结构化解析:
DOM(Document Object Model)树
Beautiful Soup:
—Python第三方库,用于从HTML或XML中提取数据
—官网:http://www.crummy.com/software/BeautifulSoup/
七、实例分析
1)目标:百度百科Python词条相关词条网页——标题和简介2)入口页:http://baike.baidu.com/view/21087.htm3)URL格式:—词条页面URL:/view/125370.htm4)数据格式:— 标题:<dd class="lemmaWgt-lemmaTitle-title"><h1>Python</h1></dd>—简介:<div class="lemma-summary" label-module="lemmaSummary">***</div>5)页面编码:UTF-8
- python爬虫入门简单爬虫
- python-简单爬虫
- 简单python爬虫
- Python简单爬虫
- python 简单爬虫
- 简单python爬虫
- Python简单的爬虫
- Python简单爬虫
- python 简单爬虫实现
- python简单文本爬虫
- python超级简单爬虫
- python简单爬虫
- python简单爬虫程序
- python简单爬虫
- Python开发简单爬虫
- Python简单爬虫
- python 简单爬虫原理
- python 3 简单爬虫
- leetcode 147. Insertion Sort List
- 适配器模式之接口适配
- 笔记——Activity生命周期
- 拼音字母
- Python常用正则表达式爬取网页信息及分析HTML标签总结
- Python简单爬虫
- Git stash: “Cannot apply to a dirty working tree, please stage your changes”
- 最最基础的最小二乘法优化
- WordPress the_excerpt()截取摘要长度
- 内核编译中的小目标
- elasticsearch mapping index 说明
- 面向对象自己的见解
- css之内边距和外边距属性
- Let'sEncrypt + Apache2.2 + Centos6 配置HTTPS实战