1: Introduction to crawlers


Contents of this article:

1. Introduction to crawlers

2. Types of reptiles

三.HTTP/HTTPS

4. HTTP/HTTPS packet capture tool Fiddler


1: Introduction to crawlers


A web crawler is a program or script that automatically crawls information from the World Wide Web according to certain rules.

Now is the era of big data. Only large companies or some companies that specialize in data collection or trading have large amounts of data. There are only two ways for ordinary people to own large amounts of non-public data. One is to purchase it, and the other is to use technology. Obtain it by yourself, such as a crawler.

There are many languages ​​​​that can be used for crawling, such as Python, Java, PHP, etc. Basically, many languages ​​​​can be used for crawling.

However, the crawler engineers currently recruited on the market are basically recruiting Python crawler projects, so the author chose to choose Python as a crawler.


Introduction to Baidu Encyclopedia: Click to open the link

Wikipedia click: open link


2. Types of reptiles


According to usage scenarios, web crawlers can be divided into two types: general crawlers and focused crawlers .


General crawlers:

General web crawlers are an important part of search engine crawling systems (Baidu, Google, Yahoo, etc.). The main purpose is to download web pages on the Internet to the local computer to form a mirror backup of Internet content.


The working principle of a universal search engine is
that a universal web crawler collects web pages and information from the Internet. This web page information is used to index the search engine to provide support. It determines whether the content of the entire engine system is rich and whether the information is real-time. Therefore, its performance directly affects the effectiveness of search engines.

Step 1:
The basic workflow of crawling web search engine web crawlers is as follows:
first select a part of the seed URL, put these URLs into the URL queue to be crawled;
take out the URL to be crawled, parse the DNS to get the IP of the host, and put The web pages corresponding to the URLs are downloaded, stored in the downloaded web page library, and these URLs are put into the crawled URL queue.
Analyze the URL in the crawled URL queue, analyze other URLs in it, and put the URL into the queue of URLs to be crawled, thus entering the next cycle....

How search engines obtain the URL of a new website:
1. The new website actively submits the URL to the search engine: (such as Baidu http://zhanzhang.baidu.com/linksubmit/url)
2. Sets up external links to the new website on other websites ( Try to be within the crawling range of search engine crawlers)
3. When search engines cooperate with DNS resolution service providers (such as DNSPod, etc.), the new website domain name will be quickly crawled.

However, certain rules are entered into the search engine spider's crawling. It needs to comply with the contents of some commands or files, such as links marked with nofollow, or the Robots protocol.
Robots protocol (also called crawler protocol, robot protocol, etc.), the full name is "Robots Exclusion Protocol", the website uses the Robots protocol to tell search engines which pages can be crawled and which pages cannot be crawled

例如:
淘宝网:https://www.taobao.com/robots.txt
腾讯网: http://www.qq.com/robots.txt

第二步:数据存储
搜索引擎通过爬虫爬取到的网页,将数据存入原始页面数据库。其中的页面数据与用户浏览器得到的HTML是完全一样的。
搜索引擎蜘蛛在抓取页面时,也做一定的重复内容检测,一旦遇到访问权重很低的网站上有大量抄袭、采集或者复制的内容,很可能就不再爬行。


第三步:预处理
搜索引擎将爬虫抓取回来的页面,进行各种步骤的预处理。
提取文字
中文分词
消除噪音(比如版权声明文字、导航条、广告等……)
索引处理
链接关系计算
特殊文件处理
....
除了HTML文件外,搜索引擎通常还能抓取和索引以文字为基础的多种文件类型,如 PDF、Word、WPS、XLS、PPT、TXT 文件等。我们在搜索结果中也经常会看到这些文件类型。
但搜索引擎还不能处理图片、视频、Flash 这类非文字内容,也不能执行脚本和程序。

第四步:提供检索服务,网站排名
搜索引擎在对信息进行组织和处理后,为用户提供关键字检索服务,将用户检索相关的信息展示给用户。
同时会根据页面的PageRank值(链接的访问量排名)来进行网站排名,这样Rank值高的网站在搜索结果中会排名较前,当然也可以直接使用 Money 购买搜索引擎网站排名,简单粗暴。



但是,这些通用性搜索引擎也存在着一定的局限性:
通用搜索引擎所返回的结果都是网页,而大多情况下,网页里90%的内容对用户来说都是无用的。
不同领域、不同背景的用户往往具有不同的检索目的和需求,搜索引擎无法提供针对具体某个用户的搜索结果。
万维网数据形式的丰富和网络技术的不断发展,图片、数据库、音频、视频多媒体等不同数据大量出现,通用搜索引擎对这些文件无能为力,不能很好地发现和获取。
通用搜索引擎大多提供基于关键字的检索,难以支持根据语义信息提出的查询,无法准确理解用户的具体需求。


针对这些情况,聚焦爬虫技术得以广泛使用。

聚焦爬虫

聚焦爬虫,是"面向特定主题需求"的一种网络爬虫程序,它与通用搜索引擎爬虫的区别在于: 聚焦爬虫在实施网页抓取时会对内容进行处理筛选,尽量保证只抓取与需求相关的网页信息。
而我们要学习的,就是聚焦爬虫。


三.HTTP/HTTPS


爬虫爬取网页,而网页通过HTTP/HTTPS请求进行加载,所以了解HTTP/HTTPS是必要的。


HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法。
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer)简单讲是HTTP的安全版,在HTTP下加入SSL层。
SSL(Secure Sockets Layer 安全套接层)主要用于Web的安全传输协议,在传输层对网络连接进行加密,保障在Internet上数据传输的安全。

HTTP的端口号为80,
HTTPS的端口号为443


HTTP工作原理

网络爬虫抓取过程可以理解为模拟浏览器操作的过程。
浏览器的主要功能是向服务器发出请求,在浏览器窗口中展示您选择的网络资源,HTTP是一套计算机通过网络进行通信的规则。

HTTP通信由两部分组成: 客户端请求消息 与 服务器响应消息


浏览器发送HTTP请求的过程:
当用户在浏览器的地址栏中输入一个URL并按回车键之后,浏览器会向HTTP服务器发送HTTP请求。HTTP请求主要分为“Get”和“Post”两种方法。

当我们在浏览器输入URL http://www.baidu.com 的时候,浏览器发送一个Request请求去获取 http://www.baidu.com 的html文件,服务器把Response文件对象发送回给浏览器。
浏览器分析Response中的 HTML,发现其中引用了很多其他文件,比如Images文件,CSS文件,JS文件。 浏览器会自动再次发送Request去获取图片,CSS文件,或者JS文件。
当所有的文件都下载成功后,网页会根据HTML语法结构,完整的显示出来了。


URL(Uniform / Universal Resource Locator的缩写):统一资源定位符,是用于完整地描述Internet上网页和其他资源的地址的一种标识方法。

基本格式:scheme://host[:port#]/path/…/[?query-string][#anchor]
scheme:协议(例如:http, https, ftp)
host:服务器的IP地址或者域名
port#:服务器的端口(如果是走协议默认端口,缺省端口80)
path:访问资源的路径
query-string:参数,发送给http服务器的数据
anchor:锚(跳转到网页的指定锚点位置)


客户端HTTP请求:

一个典型的HTTP请求示例

GET https://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.baidu.com/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Cookie: BAIDUID=04E4001F34EA74AD4601512DD3C41A7B:FG=1; BIDUPSID=04E4001F34EA74AD4601512DD3C41A7B; PSTM=1470329258; MCITY=-343%3A340%3A; BDUSS=nF0MVFiMTVLcUh-Q2MxQ0M3STZGQUZ4N2hBa1FFRkIzUDI3QlBCZjg5cFdOd1pZQVFBQUFBJCQAAAAAAAAAAAEAAADpLvgG0KGyvLrcyfrG-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFaq3ldWqt5XN; H_PS_PSSID=1447_18240_21105_21386_21454_21409_21554; BD_UPN=12314753; sug=3; sugstore=0; ORIGIN=0; bdime=0; H_PS_645EC=7e2ad3QHl181NSPbFbd7PRUCE1LlufzxrcFmwYin0E6b%2BW8bbTMKHZbDP0g; BDSVRTM=0

URL只是标识资源的位置,而HTTP是用来提交和获取资源。客户端发送一个HTTP请求到服务器的请求消息,包括以下格式:
请求行、请求头部、空行、请求数据
四个部分组成,下图给出了请求报文的一般格式。



请求方法:


GET https://www.baidu.com/ HTTP/1.1
根据HTTP标准,HTTP请求可以使用多种请求方法。
HTTP 0.9:只有基本的文本 GET 功能。
HTTP 1.0:完善的请求/响应模型,并将协议补充完整,定义了三种请求方法: GET, POST 和 HEAD方法。
HTTP 1.1:在 1.0 基础上进行更新,新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。
HTTP 2.0(未普及):请求/响应首部的定义基本没有改变,只是所有首部键必须全部小写,而且请求行要独立为 :method、:scheme、:host、:path这些键值对。

序号 方法 描述
1 GET 请求指定的页面信息,并返回实体主体。
2 HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
3 POST 向指定资源提交数据进行处理请求(例如提交表单或者上传文件),数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
4 PUT 从客户端向服务器传送的数据取代指定的文档的内容。
5 DELETE 请求服务器删除指定的页面。
6 CONNECT HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
7 OPTIONS 允许客户端查看服务器的性能。
8 TRACE 回显服务器收到的请求,主要用于测试或诊断。



HTTP请求主要分为Get和Post两种方法
GET是从服务器上获取数据,POST是向服务器传送数据
GET请求参数显示,都显示在浏览器网址上,HTTP服务器根据该请求所包含URL中的参数来产生响应内容,即“Get”请求的参数是URL的一部分。 例如: http://www.baidu.com/s?wd=Chinese
POST请求参数在请求体当中,消息长度没有限制而且以隐式的方式进行发送,通常用来向HTTP服务器提交量比较大的数据(比如请求中包含许多参数或者文件上传操作等),请求的参数包含在“Content-Type”消息头里,指明该消息体的媒体类型和编码,

注意:避免使用Get方式提交表单,因为有可能会导致安全问题。 比如说在登陆表单中用Get方式,用户输入的用户名和密码将在地址栏中暴露无遗。


常用的请求报头


1. Host (主机和端口号)
Host:对应网址URL中的Web名称和端口号,用于指定被请求资源的Internet主机和端口号,通常属于URL的一部分。
2. Connection (链接类型)
Connection:表示客户端与服务连接类型
Client 发起一个包含 Connection:keep-alive 的请求,HTTP/1.1使用 keep-alive 为默认值。
Server收到请求后:
如果 Server 支持 keep-alive,回复一个包含 Connection:keep-alive 的响应,不关闭连接;
如果 Server 不支持 keep-alive,回复一个包含 Connection:close 的响应,关闭连接。
如果client收到包含 Connection:keep-alive 的响应,向同一个连接发送下一个请求,直到一方主动关闭连接。
keep-alive在很多情况下能够重用连接,减少资源消耗,缩短响应时间,比如当浏览器需要多个文件时(比如一个HTML文件和相关的图形文件),不需要每次都去请求建立连接。
3. Upgrade-Insecure-Requests (升级为HTTPS请求)
Upgrade-Insecure-Requests:升级不安全的请求,意思是会在加载 http 资源时自动替换成 https 请求,让浏览器不再显示https页面中的http请求警报。
HTTPS 是以安全为目标的 HTTP 通道,所以在 HTTPS 承载的页面上不允许出现 HTTP 请求,一旦出现就是提示或报错。
4. User-Agent (浏览器名称)
User-Agent:是客户浏览器的名称,以后会详细讲。
5. Accept (传输文件类型)
Accept:指浏览器或其他客户端可以接受的MIME(Multipurpose Internet Mail Extensions(多用途互联网邮件扩展))文件类型,服务器可以根据它判断并返回适当的文件格式。

举例:
Accept: */*:表示什么都可以接收。
Accept:image/gif:表明客户端希望接受GIF图像格式的资源;
Accept:text/html:表明客户端希望接受html文本。
Accept: text/html, application/xhtml+xml;q=0.9, image/*;q=0.8:表示浏览器支持的 MIME 类型分别是 html文本、xhtml和xml文档、所有的图像格式资源。

q是权重系数,范围 0 =< q <= 1,q 值越大,请求越倾向于获得其“;”之前的类型表示的内容。若没有指定q值,则默认为1,按从左到右排序顺序;若被赋值为0,则用于表示浏览器不接受此内容类型。

Text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的;Application:用于传输应用程序数据或者二进制数据。详细请点击

6. Referer (页面跳转处)
Referer:表明产生请求的网页来自于哪个URL,用户是从该 Referer页面访问到当前请求的页面。这个属性可以用来跟踪Web请求来自哪个页面,是从什么网站来的等。
有时候遇到下载某网站图片,需要对应的referer,否则无法下载图片,那是因为人家做了防盗链,原理就是根据referer去判断是否是本网站的地址,如果不是,则拒绝,如果是,就可以下载;

7. Accept-Encoding(文件编解码格式)
Accept-Encoding:指出浏览器可以接受的编码方式。编码方式不同于文件格式,它是为了压缩文件并加速文件传递速度。浏览器在接收到Web响应之后先解码,然后再检查文件格式,许多情形下这可以减少大量的下载时间。

举例:Accept-Encoding:gzip;q=1.0, identity; q=0.5, *;q=0
如果有多个Encoding同时匹配, 按照q值顺序排列,本例中按顺序支持 gzip, identity压缩编码,支持gzip的浏览器会返回经过gzip编码的HTML页面。 如果请求消息中没有设置这个域服务器假定客户端对各种内容编码都可以接受。


8. Accept-Language(语言种类)
Accept-Langeuage:指出浏览器可以接受的语言种类,如en或en-us指英语,zh或者zh-cn指中文,当服务器能够提供一种以上的语言版本时要用到。

9. Accept-Charset(字符编码)
Accept-Charset:指出浏览器可以接受的字符编码。

举例:Accept-Charset:iso-8859-1,gb2312,utf-8
ISO8859-1:通常叫做Latin-1。Latin-1包括了书写所有西方欧洲语言不可缺少的附加字符,英文浏览器的默认值是ISO-8859-1.
gb2312:标准简体中文字符集;
utf-8:UNICODE 的一种变长字符编码,可以解决多种语言文本显示问题,从而实现应用国际化和本地化。
如果在请求消息中没有设置这个域,缺省是任何字符集都可以接受。

10. Cookie (Cookie)
Cookie:浏览器用这个属性向服务器发送Cookie。Cookie是在浏览器中寄存的小型数据体,它可以记载和服务器相关的用户信息,也可以用来实现会话功能,以后会详细讲。


11. Content-Type (POST数据类型)
Content-Type:POST请求里用来表示的内容类型。
举例:Content-Type = Text/XML; charset=gb2312:
指明该请求的消息体中包含的是纯文本的XML类型的数据,字符编码采用“gb2312”。



常用的响应报头(了解)

HTTP/1.1 200 OK
Server: Tengine
Connection: keep-alive
Date: Wed, 30 Nov 2016 07:58:21 GMT
Cache-Control: no-cache
Content-Type: text/html;charset=UTF-8
Keep-Alive: timeout=20
Vary: Accept-Encoding
Pragma: no-cache
X-NWS-LOG-UUID: bd27210a-24e5-4740-8f6c-25dbafa9c395
Content-Length: 180945

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ....


In theory, all response header information should be in response to request headers. However, for efficiency, security, and other considerations, the server will add corresponding response header information. As you can see from the above:


1. Cache-Control: must-revalidate, no-cache, private.
This value tells the client that the server does not want the client to cache resources. The next time a resource is requested, the server must be requested again and the resource cannot be obtained from a cached copy.
Cache-Control is very important information in the response header. When the client request header contains a Cache-Control:max-age=0 request, which clearly indicates that the server resources will not be cached, Cache-Control is usually returned as the response information. no-cache means, "then don't cache it."
When the client does not include Cache-Control in the request header, the server often determines different caching strategies for different resources. For example, oschina's strategy for caching image resources is Cache-Control: max-age=86400, which means Yes, for 86400 seconds starting from the current time, the client can read the resource directly from the cached copy without requesting it from the server.

2. Connection: keep-alive
This field responds to the client's Connection: keep-alive, telling the client that the server's tcp connection is also a long connection, and the client can continue to use this tcp connection to send http requests.

3. Content-Encoding: gzip
tells the client that the resources sent by the server are gzip-encoded. After the client sees this information, it should use gzip to decode the resources.

4. Content-Type:text/html;charset=UTF-8
Tell the client the type of resource file and character encoding. The client decodes the resource through UTF-8 and then performs HTML parsing of the resource. Usually we will see that some websites are garbled, often because the server does not return the correct encoding.

5. Date: Sun, 21 Sep 2016 06:18:21 GMT
This is the server time when the server sends resources. GMT is the standard time in Greenwich. The times sent in the http protocol are all in GMT. This is mainly to solve the problem of time confusion when different time zones request resources from each other on the Internet.

6. Expires: Sun, 1 Jan 2000 01:00:00 GMT
This response header is also related to caching. It tells the client that it can directly access the cached copy before this time. Obviously there will be problems with this value because the client and The server time is not necessarily the same, and if the time is different, it will cause problems. Therefore, this response header is not as accurate as the Cache-Control: max-age=* response header, because the date in max-age=date is a relative time, which is not only easier to understand, but also more accurate.

7. Pragma:no-cache
has the same meaning as Cache-Control.

8.Server: Tengine/1.4.6
This is the server and the corresponding version. It only tells the client the server information.

9. Transfer-Encoding:chunked

这个响应头告诉客户端,服务器发送的资源的方式是分块发送的。一般分块发送的资源都是服务器动态生成的,在发送时还不知道发送资源的大小,所以采用分块发送,每一块都是独立的,独立的块都能标示自己的长度,最后一块是0长度的,当客户端读到这个0长度的块时,就可以确定资源已经传输完了。

10. Vary: Accept-Encoding
告诉缓存服务器,缓存压缩文件和非压缩文件两个版本,现在这个字段用处并不大,因为现在的浏览器都是支持压缩的。

响应状态码
响应状态代码有三位数字组成,第一个数字定义了响应的类别,且有五种可能取值。
常见状态码
100~199:表示服务器成功接收部分请求,要求客户端继续提交其余请求才能完成整个处理过程。
200~299:表示服务器成功接收请求并已完成整个处理过程。常用200(OK 请求成功)。
300~399:为完成请求,客户需进一步细化请求。例如:请求的资源已经移动一个新地址、常用302(所请求的页面已经临时转移至新的url)、307和304(使用缓存资源)。
400~499:客户端的请求有错误,常用404(服务器无法找到被请求的页面)、403(服务器拒绝访问,权限不够)。
500~599:服务器端出现错误,常用500(请求未完成。服务器遇到不可预知的情况)。

Cookie 和 Session
服务器和客户端的交互仅限于请求/响应过程,结束之后便断开,在下一次请求时,服务器会认为新的客户端。
为了维护他们之间的链接,让服务器知道这是前一个用户发送的请求,必须在一个地方保存客户端的信息。


Cookie:通过在 客户端 记录的信息确定用户的身份。

Session:通过在 服务器端 记录的信息确定用户的身份。


四.HTTP/HTTPS抓包工具Fiddler


下载地址:https://www.telerik.com/download/fiddler


请求 (Request) 部分详解
Headers —— 显示客户端发送到服务器的 HTTP 请求的 header,显示为一个分级视图,包含了 Web 客户端信息、Cookie、传输状态等。
Textview —— 显示 POST 请求的 body 部分为文本。
WebForms —— 显示请求的 GET 参数 和 POST body 内容。
HexView —— 用十六进制数据显示请求。
Auth —— 显示响应 header 中的 Proxy-Authorization(代理身份验证) 和 Authorization(授权) 信息.
Raw —— 将整个请求显示为纯文本。
JSON - 显示JSON格式文件。
XML —— 如果请求的 body 是 XML 格式,就是用分级的 XML 树来显示它。


响应 (Response) 部分详解
Transformer —— 显示响应的编码信息。
Headers —— 用分级视图显示响应的 header。
TextView —— 使用文本显示相应的 body。
ImageVies —— 如果请求是图片资源,显示响应的图片。
HexView —— 用十六进制数据显示响应。
WebView —— 响应在 Web 浏览器中的预览效果。
Auth —— 显示响应 header 中的 Proxy-Authorization(代理身份验证) 和 Authorization(授权) 信息。
Caching —— 显示此请求的缓存信息。
Privacy —— 显示此请求的私密 (P3P) 信息。
Raw —— 将整个响应显示为纯文本。
JSON - 显示JSON格式文件。
XML —— 如果响应的 body 是 XML 格式,就是用分级的 XML 树来显示它 。



官网视频教程(可能需翻墙):

http://www.telerik.com/videos/fiddler/getting-started-with-fiddler-web-debugging-proxy





Guess you like

Origin blog.csdn.net/qq_33613696/article/details/77510095