博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【爬虫】Python2 爬虫初学笔记
阅读量:4365 次
发布时间:2019-06-07

本文共 2641 字,大约阅读时间需要 8 分钟。

  

 

爬虫,个人理解就是:利用模拟“操作浏览器”的过程,自动获取我们想要的数据(或者说信息,比如图片啊)

为何要学爬虫:爬取数据,为我所用(相当于可以把一类数据整合起来)

一.简单静态网页爬虫架构:

  1.Background Knowledge:URL(统一资源定位符,能帮助我们定位到网页在网络中的位置,URI 是统一资源标志符),HTTP协议

  2.构架:

  需要一个爬虫调度器管理下面的程序,涉及多线程管理等(比如说申请网页的阻塞时间可以用来建立新的申请,这些资源分配由操作系统完成)

  URL管理器,防止URL重复使用,获取URL,未爬取和已爬取的管理  

 

  

  3.工作流程:

  4.URL管理器实现方式:

    a.存储在内存(set)

    b.关系数据库(可永久保存)

    c.缓存数据库(大部分公司使用这种方式)

  5.网页下载器:

    以HTML形式保存网页,可以使用urllib和urllib2实现下载

    实现方法:

    a.简单的使用urllib2.open(url)

    b.添加Request方法,发送包头,伪装成浏览器

    c.添加cookiejar cookie 容器

  

1 # coding=utf-8 2 import urllib2 3 import cookielib 4 url = "http://www.baidu.com" 5 print '方法1' 6 #请确保url 的合法性 7 response1 = urllib2.urlopen(url) 8 if response1.getcode()==200: 9     print ' 读取网页成功'10     print ' Length:',11     print len(response1.read())12 else:13     print ' 读取网页失败'14 15 print 'Method2:'16 request = urllib2.Request(url)17 request.add_header("usr_agent","Mozilla/6.0")18 response2 = urllib2.urlopen(request)19 if response2.getcode()==200:20     print ' 读取网页成功'21     print ' Length:',22     print len(response2.read())23 else:24     print ' 读取网页失败'25 26 print 'Method3:'27 cj = cookielib.CookieJar()28 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))29 urllib2.install_opener(opener)30 response3 = urllib2.urlopen(url)31 if response3.getcode()==200:32     print ' 读取网页成功'33     print ' Length:',34     print len(response3.read())35     print cj36     print response3.read()37 else:38     print ' 读取网页失败'
View Code

  6.网页解析器:

  以下载好的HTML当成字符串,查找出

  1.正则表达式匹配

  2.html.parser

   3.lxml解析器

  4.BeautifulSoup

   以DOM(Document Object Model) 结构化解析,下面是其语法

  

1 # coding=utf-8 2 import re 3  4 from bs4 import BeautifulSoup 5 html_doc = """ 6 The Dormouse's story 7  8 

The Dormouse's story

9 10

Once upon a time there were three little sisters; and their names were11 Elsie,12 Lacie and13 Tillie;14 and they lived at the bottom of a well.

15 16

...

17 """18 #创建19 ccsSoup = BeautifulSoup(html_doc,'html.parser',from_encoding='utf8')20 #获取所有链接21 links= ccsSoup.find_all('a')22 for link in links:23 print link.name,link['href'],link.get_text()24 print ccsSoup.p('class')25 26 print '正则匹配'27 link_node = ccsSoup.find('a',href= re.compile(r"h"),class_='sister')28 print link_node29 link_node = ccsSoup.find('a',href= re.compile(r"d"))30 print link_node

  5.调度程序

 

 

 

参考:  

    http://www.imooc.com/video/10686

    https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

    正则表达式:

      http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

    PyCharm:使用教程

    http://blog.csdn.net/pipisorry/article/details/39909057

转载于:https://www.cnblogs.com/guiguzhixing/p/5860239.html

你可能感兴趣的文章
JavaWeb学习总结(四十九)——简单模拟Sping MVC
查看>>
XOR (莫队)
查看>>
开发环境,生产环境,测试环境的区别
查看>>
|洛谷|排序|P1309 瑞士轮
查看>>
Java简介
查看>>
简单排序实现
查看>>
.Net程序员学用Oracle系列(7):视图、函数、存储过程、包
查看>>
setStreamMute无法Mute部分stream
查看>>
Android 绑定类型服务---绑定服务
查看>>
bzoj 4555 求和
查看>>
Spring的工作原理
查看>>
四分树 (Quadtrees UVA - 297)
查看>>
Quartz 学习
查看>>
获取项目路径
查看>>
[第1组]头脑风暴+核心竞争力+功能集+NABCD
查看>>
E20180518-hm
查看>>
jwt vs session
查看>>
ThinkPHP关联模型详解
查看>>
(转)vue入门
查看>>
Ajax写分页查询(实现不刷新页面)
查看>>