2019 网络爬虫和相关工具

栏目: 编程工具 · 发布时间: 6年前

内容简介:网络爬虫(web crawler),以前经常称之为网络蜘蛛(spider),是按照一定的规则自动浏览万维网并获取信息的机器人程序(或脚本),曾经被广泛的应用于互联网搜索引擎。使用过互联网和浏览器的人都知道,网页中除了供用户阅读的文字信息之外,还包含一些超链接。网络爬虫系统正是通过网页中的超链接信息不断获得网络上的其它页面。正因如此,网络数据采集的过程就像一个爬虫或者蜘蛛在网络上漫游,所以才被形象的称为网络爬虫或者网络蜘蛛。在理想的状态下,所有ICP(Internet Content Provider)都应该

  2019 网络爬虫和相关工具

网络爬虫

网络爬虫(web crawler),以前经常称之为网络蜘蛛(spider),是按照一定的规则自动浏览万维网并获取信息的机器人程序(或脚本),曾经被广泛的应用于互联网搜索引擎。使用过互联网和浏览器的人都知道,网页中除了供用户阅读的文字信息之外,还包含一些超链接。网络爬虫系统正是通过网页中的超链接信息不断获得网络上的其它页面。正因如此,网络数据采集的过程就像一个爬虫或者蜘蛛在网络上漫游,所以才被形象的称为网络爬虫或者网络蜘蛛。

爬虫的应用领域

在理想的状态下,所有ICP(Internet Content Provider)都应该为自己的网站提供API接口来共享它们允许其他程序获取的数据,在这种情况下爬虫就不是必需品,国内比较有名的电商平台(如淘宝、京东等)、社交平台(如腾讯微博等)等网站都提供了自己的Open API,但是这类Open API通常会对可以抓取的数据以及抓取数据的频率进行限制。对于大多数的公司而言,及时的获取行业相关数据是企业生存的重要环节之一,然而大部分企业在行业数据方面的匮乏是其与生俱来的短板,合理的利用爬虫来获取数据并从中提取出有商业价值的信息是至关重要的。当然爬虫还有很多重要的应用领域,下面列举了其中的一部分:

  1. 搜索引擎
  2. 新闻聚合
  3. 社交应用
  4. 舆情监控
  5. 行业数据

合法性和背景调研

爬虫合法性探讨

网络爬虫领域目前还属于拓荒阶段,虽然互联网世界已经通过自己的游戏规则建立起一定的道德规范(Robots协议,全称是“网络爬虫排除标准”),但法律部分还在建立和完善中,也就是说,现在这个领域暂时还是灰色地带。

“法不禁止即为许可”,如果爬虫就像浏览器一样获取的是前端显示的数据(网页上的公开信息)而不是网站后台的私密敏感信息,就不太担心法律法规的约束,因为目前大数据产业链的发展速度远远超过了法律的完善程度。

在爬取网站的时候,需要限制自己的爬虫遵守Robots协议,同时控制网络爬虫程序的抓取数据的速度;在使用数据的时候,必须要尊重网站的知识产权(从Web 2.0时代开始,虽然Web上的数据很多都是由用户提供的,但是网站平台是投入了运营成本的,当用户在注册和发布内容时,平台通常就已经获得了对数据的所有权、使用权和分发权)。如果违反了这些规定,在打官司的时候败诉几率相当高。

Robots.txt文件

大多数网站都会定义robots.txt文件,下面以淘宝的robots.txt文件为例,看看该网站对爬虫有哪些限制。

User-agent:  Baiduspider 
Allow:  /article 
Allow:  /oshtml 
Disallow:  /product/ 
Disallow:  / 
 
User-Agent:  Googlebot 
Allow:  /article 
Allow:  /oshtml 
Allow:  /product 
Allow:  /spu 
Allow:  /dianpu 
Allow:  /oversea 
Allow:  /list 
Disallow:  / 
 
User-agent:  Bingbot 
Allow:  /article 
Allow:  /oshtml 
Allow:  /product 
Allow:  /spu 
Allow:  /dianpu 
Allow:  /oversea 
Allow:  /list 
Disallow:  / 
 
User-Agent:  360Spider 
Allow:  /article 
Allow:  /oshtml 
Disallow:  / 
 
User-Agent:  Yisouspider 
Allow:  /article 
Allow:  /oshtml 
Disallow:  / 
 
User-Agent:  Sogouspider 
Allow:  /article 
Allow:  /oshtml 
Allow:  /product 
Disallow:  / 
 
User-Agent:  Yahoo!  Slurp 
Allow:  /product 
Allow:  /spu 
Allow:  /dianpu 
Allow:  /oversea 
Allow:  /list 
Disallow:  / 
 
User-Agent:  * 
Disallow:  / 

注意上面robots.txt第一段的最后一行,通过设置“Disallow: /”禁止百度爬虫访问除了“Allow”规定页面外的其他所有页面。因此当你在百度搜索“淘宝”的时候,搜索结果下方会出现:“由于该网站的robots.txt文件存在限制指令(限制搜索引擎抓取),系统无法提供该页面的内容描述”。百度作为一个搜索引擎,至少在表面上遵守了淘宝网的robots.txt协议,所以用户不能从百度上搜索到淘宝内部的产品信息。

2019 网络爬虫和相关工具

相关 工具 介绍

HTTP协议

在开始讲解爬虫之前,我们稍微对HTTP(超文本传输协议)做一些回顾,因为我们在网页上看到的内容通常是浏览器执行HTML语言得到的结果,而HTTP就是传输HTML数据的协议。HTTP和其他很多应用级协议一样是构建在TCP(传输控制协议)之上的,它利用了TCP提供的可靠的传输服务实现了Web应用中的数据交换。按照维基百科上的介绍,设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法,也就是说这个协议是浏览器和Web服务器之间传输的数据的载体。关于这个协议的详细信息以及目前的发展状况,大家可以阅读阮一峰老师的《HTTP 协议入门》、《互联网协议入门》系列以及《图解HTTPS协议》进行了解,下图是我在四川省网络通信技术重点实验室工作期间用开源协议分析工具Ethereal(抓包工具WireShark的前身)截取的访问百度首页时的HTTP请求和响应的报文(协议数据),由于Ethereal截取的是经过网络适配器的数据,因此可以清晰的看到从物理链路层到应用层的协议数据。

HTTP请求(请求行+请求头+空行+[消息体]):

2019 网络爬虫和相关工具

HTTP响应(响应行+响应头+空行+消息体):

2019 网络爬虫和相关工具

说明:但愿这两张如同泛黄的照片般的截图帮助你大概的了解到HTTP是一个怎样的协议。

相关工具

1.Chrome Developer Tools:谷歌浏览器内置的开发者工具。

2019 网络爬虫和相关工具

2.POSTMAN:功能强大的网页调试与RESTful请求工具。

2019 网络爬虫和相关工具

3.

2019 网络爬虫和相关工具

4.HTTPie:命令行HTTP客户端。

$ http --header http://www.scu.edu.cn 
HTTP/1.1 200 OK 
Accept-Ranges: bytes 
Cache-Control: private, max-age=600 
Connection: Keep-Alive 
Content-Encoding: gzip 
Content-Language: zh-CN 
Content-Length: 14403 
Content-Type: text/html 
Date: Sun, 27 May 2018 15:38:25 GMT 
ETag: "e6ec-56d3032d70a32-gzip" 
Expires: Sun, 27 May 2018 15:48:25 GMT 
Keep-Alive: timeout=5, max=100 
Last-Modified: Sun, 27 May 2018 13:44:22 GMT 
Server: VWebServer 
Vary: User-Agent,Accept-Encoding 
X-Frame-Options: SAMEORIGIN 

5.BuiltWith:识别网站所用技术的工具。

>>> import builtwith 
>>> builtwith.parse('http://www.bootcss.com/') 
{'web-servers': ['Nginx'], 'font-scripts': ['Font Awesome'], 'javascript-frameworks': ['Lo-dash', 'Underscore.js', 'Vue.js', 'Zepto', 'jQuery'], 'web-frameworks': ['Twitter Bootstrap']} 
>>> 
>>> import ssl 
>>> ssl._create_default_https_context = ssl._create_unverified_context 
>>> builtwith.parse('https://www.jianshu.com/') 
{'web-servers': ['Tengine'], 'web-frameworks': ['Twitter Bootstrap', 'Ruby on Rails'], 'programming-languages': ['Ruby']} 

6.python-whois:查询网站所有者的工具。

>>> import whois 
>>> whois.whois('baidu.com') 
{'domain_name': ['BAIDU.COM', 'baidu.com'], 'registrar': 'MarkMonitor, Inc.', 'whois_server': 'whois.markmonitor.com', 'referral_url': None, 'updated_date': [datetime.datetime(2017, 7, 28, 2, 36, 28), datetime.datetime(2017, 7, 27, 19, 36, 28)], 'creation_date': [datetime.datetime(1999, 10, 11, 11, 5, 17), datetime.datetime(1999, 10, 11, 4, 5, 17)], 'expiration_date': [datetime.datetime(2026, 10, 11, 11, 5, 17), datetime.datetime(2026, 10, 11, 0, 0)], 'name_servers': ['DNS.BAIDU.COM', 'NS2.BAIDU.COM', 'NS3.BAIDU.COM', 'NS4.BAIDU.COM', 'NS7.BAIDU.COM', 'dns.baidu.com', 'ns4.baidu.com', 'ns3.baidu.com', 'ns7.baidu.com', 'ns2.baidu.com'], 'status': ['clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited', 'clientTransferProhibited https://icann.org/epp#clientTransferProhibited', 'clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited', 'serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited', 'serverTransferProhibited https://icann.org/epp#serverTransferProhibited', 'serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited', 'clientUpdateProhibited (https://www.icann.org/epp#clientUpdateProhibited)', 'clientTransferProhibited (https://www.icann.org/epp#clientTransferProhibited)', 'clientDeleteProhibited (https://www.icann.org/epp#clientDeleteProhibited)', 'serverUpdateProhibited (https://www.icann.org/epp#serverUpdateProhibited)', 'serverTransferProhibited (https://www.icann.org/epp#serverTransferProhibited)', 'serverDeleteProhibited (https://www.icann.org/epp#serverDeleteProhibited)'], 'emails': ['abusecomplaints@markmonitor.com', 'whoisrelay@markmonitor.com'], 'dnssec': 'unsigned', 'name': None, 'org': 'Beijing Baidu Netcom Science Technology Co., Ltd.', 'address': None, 'city': None, 'state': 'Beijing', 'zipcode': None, 'country': 'CN'} 

7.robotparser:解析robots.txt的工具。

>>> from urllib import robotparser 
>>> parser = robotparser.RobotFileParser() 
>>> parser.set_url('https://www.taobao.com/robots.txt') 
>>> parser.read() 
>>> parser.can_fetch('Hellokitty', 'http://www.taobao.com/article') 
False 
>>> parser.can_fetch('Baiduspider', 'http://www.taobao.com/article') 
True 
>>> parser.can_fetch('Baiduspider', 'http://www.taobao.com/product') 
False 

一个简单的爬虫

一个基本的爬虫通常分为数据采集(网页下载)、数据处理(网页解析)和数据存储(将有用的信息持久化)三个部分的内容,当然更为高级的爬虫在数据采集和处理时会使用并发编程或分布式技术,这就需要有调度器(安排线程或进程执行对应的任务)、后台管理程序(监控爬虫的工作状态以及检查数据抓取的结果)等的参与。

2019 网络爬虫和相关工具

一般来说,爬虫的工作流程包括以下几个步骤:

  1. 设定抓取目标(种子页面/起始页面)并获取网页。
  2. 当服务器无法访问时,按照指定的重试次数尝试重新下载页面。
  3. 在需要的时候设置用户代理或隐藏真实IP,否则可能无法访问页面。
  4. 对获取的页面进行必要的解码操作然后抓取出需要的信息。
  5. 在获取的页面中通过某种方式(如正则表达式)抽取出页面中的链接信息。
  6. 对链接进行进一步的处理(获取页面并重复上面的动作)。
  7. 将有用的信息进行持久化以备后续的处理。

下面的例子给出了一个从“搜狐体育”上获取NBA新闻标题和链接的爬虫。

from urllib.error import URLError 
from urllib.request import urlopen 
 
import re 
import pymysql 
import ssl 
 
from pymysql import Error 
 
 
# 通过指定的字符集对页面进行解码(不是每个网站都将字符集设置为utf-8) 
def decode_page(page_bytes, charsets=('utf-8',)): 
    page_html = None 
    for charset in charsets: 
        try: 
            page_html = page_bytes.decode(charset) 
            break 
        except UnicodeDecodeError: 
            pass 
            # logging.error('Decode:', error) 
    return page_html 
 
 
# 获取页面的HTML代码(通过递归实现指定次数的重试操作) 
def get_page_html(seed_url, *, retry_times=3, charsets=('utf-8',)): 
    page_html = None 
    try: 
        page_html = decode_page(urlopen(seed_url).read(), charsets) 
    except URLError: 
        # logging.error('URL:', error) 
        if retry_times > 0: 
            return get_page_html(seed_url, retry_times=retry_times - 1, 
                                 charsets=charsets) 
    return page_html 
 
 
# 从页面中提取需要的部分(通常是链接也可以通过正则表达式进行指定) 
def get_matched_parts(page_html, pattern_str, pattern_ignore_case=re.I): 
    pattern_regex = re.compile(pattern_str, pattern_ignore_case) 
    return pattern_regex.findall(page_html) if page_html else [] 
 
 
# 开始执行爬虫程序并对指定的数据进行持久化操作 
def start_crawl(seed_url, match_pattern, *, max_depth=-1): 
    conn = pymysql.connect(host='localhost', port=3306, 
                           database='crawler', user='root', 
                           password='123456', charset='utf8') 
    try: 
        with conn.cursor() as cursor: 
            url_list = [seed_url] 
            # 通过下面的字典避免重复抓取并控制抓取深度 
            visited_url_list = {seed_url: 0} 
            while url_list: 
                current_url = url_list.pop(0) 
                depth = visited_url_list[current_url] 
                if depth != max_depth: 
                    # 尝试用utf-8/gbk/gb2312三种字符集进行页面解码 
                    page_html = get_page_html(current_url, charsets=('utf-8', 'gbk', 'gb2312')) 
                    links_list = get_matched_parts(page_html, match_pattern) 
                    param_list = [] 
                    for link in links_list: 
                        if link not in visited_url_list: 
                            visited_url_list[link] = depth + 1 
                            page_html = get_page_html(link, charsets=('utf-8', 'gbk', 'gb2312')) 
                            headings = get_matched_parts(page_html, r'<h1>(.*)<span') 
                            if headings: 
                                param_list.append((headings[0], link)) 
                    cursor.executemany('insert into tb_result values (default, %s, %s)', 
                                       param_list) 
                    conn.commit() 
    except Error: 
        pass 
        # logging.error('SQL:', error) 
    finally: 
        conn.close() 
 
 
def main(): 
    ssl._create_default_https_context = ssl._create_unverified_context 
    start_crawl('http://sports.sohu.com/nba_a.shtml', 
                r'<a[^>]+test=a\s[^>]*href=["\'](.*?)["\']', 
                max_depth=2) 
 
 
if __name__ == '__main__': 
    main() 

由于使用了 MySQL 实现持久化操作,所以要先启动MySQL服务器再运行该程序。


以上所述就是小编给大家介绍的《2019 网络爬虫和相关工具》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码