python如何实现爬虫技术

一、什么是爬虫

爬虫:爬虫的作用就是从互联网上抓取对我们有价值的信息。他的本质,是一段程序,是一段自动抓取互联网信息的程序。

二、Python爬虫架构

Python 爬虫架构主要由调度器、URL管理器、网页下载器、网页解析器、应用程序五个部分组成。

调度器:

调度器主要负责调度URL管理器、下载器、解析器之间的协调,作用相当于电脑的CPU

URL管理器:

url管理器包括已经抓取和还在等待爬取的url地址,防止重复和循环抓取url.实现url管理器主要有三种方式,方式1内存,方式2数据库,方式3缓存数据。

网页下载器:

下载网页要通过传入一个url地址来进行,将网页转换成一个字符串,网页下载器有urllib2(Python官方基础模块)包括需要登录、代理、和cookie,requests(第三方包)

网页解析器:

将一个网页字符串进行解析,可以按照我们的要求来提取出我们有用的信息,也可以根据DOM树的解析方式来解析。网页解析器有正则表达式(直观,将网页转成字符串通过模糊匹配的方式来提取有价值的信息,当文档比较复杂的时候,该方法提取数据的时候就会非常的困难)、html.parser(Python自带的)、beautifulsoup(第三方插件,可以使用Python自带的html.parser进行解析,也可以使用lxml进行解析,相对于其他几种来说要强大一些)、lxml(第三方插件,可以解析 xml 和 HTML),html.parser 和 beautifulsoup 以及 lxml 都是以 DOM 树的方式进行解析的。

应用程序:

从网页中提取的有用数据组成的一个应用。

下面用一个图来解释一下调度器是如何协调工作的:

三、urllib2 实现下载网页的三种方式

#!/usr/bin/python

# -*- coding: UTF-8 -*-

importcookielib

importurllib2

url ="http://www.baidu.com"

response1 = urllib2.urlopen(url)

print"第一种方法"

#获取状态码,200表示成功

printresponse1.getcode()

#获取网页内容的长度

printlen(response1.read())

print"第二种方法"

request = urllib2.Request(url)

#模拟Mozilla浏览器进行爬虫

request.add_header("user-agent","Mozilla/5.0")

response2 = urllib2.urlopen(request)

printresponse2.getcode()

printlen(response2.read())

print"第三种方法"

cookie = cookielib.CookieJar()

#加入urllib2处理cookie的能力

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

urllib2.install_opener(opener)

response3 = urllib2.urlopen(url)

printresponse3.getcode()

printlen(response3.read())

printcookie

四、第三方库 Beautiful Soup 的安装

Beautiful Soup: Python 的第三方插件用来提取 xml 和 HTML 中的数据,官网地址https://www.crummy.com/software/BeautifulSoup/

1、安装 Beautiful Soup

打开 cmd(命令提示符),进入到 Python(Python2.7版本)安装目录中的 scripts 下,输入 dir 查看是否有 pip.exe, 如果用就可以使用 Python 自带的 pip 命令进行安装,输入以下命令进行安装即可:

pipinstallbeautifulsoup4

2、测试是否安装成功

编写一个 Python 文件,输入:

import bs4

print bs4

运行该文件,如果能够正常输出则安装成功。

五、使用 Beautiful Soup 解析 html 文件

#!/usr/bin/python

# -*- coding: UTF-8 -*-

importre

frombs4importBeautifulSoup

html_doc ="""

<html><head><title>The Dormouse's story</title></head>

<body>

<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were

<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,

<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and

<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;

and they lived at the bottom of a well.</p>

<p class="story">...</p>

"""

#创建一个BeautifulSoup解析对象

soup = BeautifulSoup(html_doc,"html.parser",from_encoding="utf-8")

#获取所有的链接

links = soup.find_all('a')

print"所有的链接"

forlinkinlinks:

printlink.name,link['href'],link.get_text()

print"获取特定的URL地址"

link_node = soup.find('a',href="http://example.com/elsie")

printlink_node.name,link_node['href'],link_node['class'],link_node.get_text()

print"正则表达式匹配"

link_node = soup.find('a',href=re.compile(r"ti"))

printlink_node.name,link_node['href'],link_node['class'],link_node.get_text()

print"获取P段落的文字"

p_node = soup.find('p',class_='story')

printp_node.name,p_node['class'],p_node.get_text()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python爬虫入门(urllib+Beautifulsoup) 本文包括:1、爬虫简单介绍2、爬虫架构三大模块3...
    廖少少阅读 9,931评论 0 6
  • 前言 初次接触Python,是以为测试同事用来做自动化测试,这两天有空“研究”了一下Python网络爬虫,所谓“研...
    yuyangkk阅读 1,867评论 0 0
  • Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def...
    伊森H阅读 3,083评论 0 15
  • 用于python面试整理,主要来源于http://www.cnblogs.com/skiler/p/6952707...
    十里江城阅读 2,382评论 0 13
  • 溪水急着要流向海洋 浪潮却渴望重回土地 在绿树百花的篱前 曾那样轻易的挥手道别 而沧桑的二十年后 我们的魂魄却夜夜...
    露珠拾遗阅读 299评论 0 0