网络爬虫( 网页蜘蛛,网络机器人,网页追逐者,自动索引,模拟程序)是一种按照必定的规则自动地抓取互联网信息的程序或者脚本,从互联网上抓取对于咱们有价值的信息。Tips:自动提取网页的程序,为搜索引擎从万维网上下载网页,是搜索引擎的重要组成。html
(1) 对抓取目标的描述或定义;node
(2) 对网页或数据的分析与过滤;python
(3) 对URL的搜索策略。正则表达式
Python爬虫架构主要由调度器、URL管理器、网页下载器、网页解析器、应用程序(爬取的有价值数据)5个部分组成。数据库
下面用一个图来解释一下调度器是如何协调工做的:网页爬虫
方法一:使用urllib.request.urlopen(url)方法函数实现最基本请求url的发起(打开url网址的操做)浏览器
函数原型以下:urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) 缓存
方法二:使用response=urllib.request. Request (url)及urllib.request.urlopen(request)函数bash
response=urllib.request. Request (url)实现对目标url,data,headers以及method访问cookie
urllib.request.urlopen(request)参数为request对象,代码中 response就是上一步获得的request对象(打开url网址的操做)
Tips:构建一个完整的请求,若是请求中须要加入headers(请求头)等信息,咱们就须要使用更强大的Request类来构建一个请求。Request存在的意义是便于在请求的时候传入一些信息,而urlopen则不。
方法三:加入urllib.request处理cookie的能力结合urllib.request.urlopen(url)函数实现
Tips:Python 2使用urllib2代替urllib.request,cookies代替http.cookiejar,print代替print()
#!/usr/bin/python # -*- coding: UTF-8 -*- import http.cookiejar import urllib.request url = "http://www.baidu.com" response1 = urllib.request.urlopen(url) print ("第一种方法") # 获取状态码,200表示成功 print (response1.getcode()) # 获取网页内容的长度 print (len(response1.read())) print ("第二种方法") request = urllib.request.Request(url) # 模拟Mozilla浏览器进行爬虫 request.add_header("user-agent", "Mozilla/5.0") response2 = urllib.request.urlopen(request) print (response2.getcode()) print (len(response2.read())) print ("第三种方法") cookie=http.cookiejar.CookieJar() # 加入urllib.request处理cookie的能力 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) urllib.request.install_opener(opener) response3 = urllib.request.urlopen(url) print (response3.getcode()) print (len(response3.read())) print (cookie)
执行结果见下图:
Beautiful Soup:Python 的第三方插件,用来提取 xml 和 HTML 中的数据,官网地址 https://www.crummy.com/software/BeautifulSoup/。
打开cmd(命令提示符),进入到Python(Python3版本)安装目录中的Scripts下,输入dir查看是否有pip.exe,若是用就可使用Python自带的pip命令进行安装,输入如下命令进行安装便可:
pip install beautifulsoup4
执行以下图:
2、测试是否安装成功
编写一个 Python 文件test.py,输入:
import bs4 print (bs4)
运行该文件,若是可以正常输出则安装成功,以下。
4.2 使用 Beautiful Soup 解析 html 文件
#!/usr/bin/python # -*- coding: UTF-8 -*- import re from bs4 import BeautifulSoup 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://news.baidu.com" name="tj_trnews" class="mnav">新闻</a> <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a> <a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a> <a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a> <a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">贴吧</a> <a href="http://xueshu.baidu.com" name="tj_trxueshu" class="mnav">学术</a> and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立一个BeautifulSoup解析对象 soup = BeautifulSoup(html_doc, "html.parser") # 获取全部的连接 links = soup.find_all('a') print ("全部的连接") for link in links: print (link.name, link['href'], link.get_text()) print ("获取特定的URL地址") link_node = soup.find('a', href="http://news.baidu.com") print (link_node.name, link_node['href'], link_node['class'], link_node.get_text()) print ("正则表达式匹配") link_node = soup.find('a', href=re.compile(r"hao")) print (link_node.name, link_node['href'], link_node['class'], link_node.get_text()) print ("获取P段落的文字") p_node = soup.find('p', class_='story') print (p_node.name, p_node['class'], p_node.get_text())
执行结果以下: