Scrapy
爬虫:自动抓取互联网上信息的程序,有效地利用互联网上的数据;简单爬虫架构如下:
- Url管理器:是为了防止重复抓取和循环抓取,其实现方式有三种:内存(Python内存,set()方法)、关系型数据库(MySQL, urls(url,is_crawled)、缓存数据库(redis)。
-
网页下载器:将Url对应的网页下载到本地,存储成本地文件或内存字符串,Python的文件下载器有:urllib2(Python基础模块)、requests(第三方包):
下载网页的方法:
方法一:
import urllib2
response = urllib2.urlopen('http://www.baidu.com')
print response.getcode() #返回200表示获取成功
cont = response.read() #读取内容
方法二:
import urllib2
request = urllib2.Request(url) #创建Requset对象
request.add_data('a','1') #添加数据
request.add_header('User-Agent','Mozilla/5.0') #添加http的header
response = urllib2.urlopen(request) #发送请求获取结果
特殊情景的处理器
HTTPCookieProcessor ProxyHandler HTTPSHandler HTTPRedirectHandler
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
urllib2.urlopen(url)
urllib2.urlopen(request)
例:cookie的处理
import urllib2,cookielib
cj = cookielib.CookieJar() #创建cookie容器
opener = urllib2.build_opener(urllib2,HTTPCookieProcessor(cj)) #创建一个opener
urllib2.install_opener(opener) #给urllib2安装opener
response = urllib2.urlopen("http://www.baidu.com/") #使用带有cookie的urllib2访问网页
三种方法的测试
import urllib2,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(request)
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()
结果如下: ———-
- 网页解析器:从网页中提取有价值数据的工具,Python的网页解析器一般有:正则表达式(模糊匹配)、html.parser(自带模块)、Beautiful Soup(第三方插件)、lxml(第三方插件),html.parser, Beautiful Soup和lxml为结构化解析(Document Object Model, DOM树),Dom树的结构如下:
Beautiful Soup 第三方库,用于从HTML或XML中提取数据。
安装:
python -m pip install beautifulsoup4
测试:
import bs4
print bs4
Beautiful Soup 的语法
测试代码:
# -*- coding: GBK-*-
from bs4 import BeautifulSoup
import re
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>
"""
soup = BeautifulSoup(html_doc,'html.parser',from_encoding = 'utf-8')
print '获取所有的链接'
links = soup.find_all('a')
for link in links:
print link.name, link['href'],link.get_text()
print '获取lacie的链接'
link_node = soup.find('a',href = 'http://example.com/lacie')
print link_node.name,link_node['href'],link_node.get_text()
print "正则匹配"
link_node = soup.find('a',href=re.compile(r"ill"))
print link_node.name,link_node['href'],link_node.get_text()
print '获取P段落文字'
p_node = soup.find('p',class_="title")
print p_node.name,p_node.get_text()
结果:
Ref:[1] Python开发简单爬虫项目