Python爬虫最为核心的HTTP协议解析,及自定义协议的分析!
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python爬虫最为核心的HTTP协议解析,及自定义协议的分析!,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4653字,纯文字阅读大概需要7分钟。
内容图文
![Python爬虫最为核心的HTTP协议解析,及自定义协议的分析!](/upload/InfoBanner/zyjiaocheng/843/28208d82d78a4ea2b8a8feaffaacf27f.jpg)
机器之间的协议就是机器通信的语法,只有按照这种语法发来的信息,机器之间才能相互理解内容,也可以理解为信息的一种格式。
HTTP/IP协议是互联网最为重要的协议,没有HTTP/IP协议,也就没有互联跟不会有网,对于爬虫而言一切数据、请求都是围绕HTTP协议展开。
但是在python实现的网络爬虫中都是使用封装好了的请求库如:requests、scrapy、urllib等,这些是对socket的封装,而socket是除了机器语言外最底层的协议。
HTTP是公认的协议,但是并不是所有的终端通信都使用HTTP协议,也有处于保密需求而自定义协议,我们要通过对HTTP协议的分析理解来认来掌握自定义协议的分析思路。
在浏览器开发者模式下,任意截获一个数据包点击view parsed,显示出来的就是原始的HTTP请求头格式及协议请求头格式。
最主要的头两行分析如下,第一行:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> GET / HTTP/1.1 分别是请求方式 请求路径 协议及其版本
</pre>
/就表示首页,最后的HTTP/1.1指示采用的HTTP协议版本是1.1
从第二行开始,每一行都类似于Xxx: abcdefg:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Host: mzzd.xyz
</pre>
表示请求的域名是mzzd.xyz,如果一台服务器有多个网站,服务器就需要通过Host来区分浏览器请求的是哪个网站。
再看HTTP响应及其格式:
HTTP响应分为Header和Body两部分(Body是可选项),我们在Network中看到的Header最重要的几行如下:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> HTTP/1.1 200 OK 分别是协议版本 状态码 说明
</pre>
200表示一个成功的响应,后面的OK是说明。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Content-Type: text/html
</pre>
Content-Type指示响应的内容,浏览器依靠Content-Type来判断响应的内容类型,即使URL是http://www.mzzd.xyz/1.jpg,它也不一定就是图片。
那我们用python实现HTTP客户端:
这个客户端的作用就是当你在浏览器访问本地的8000端口,会向浏览器返回hello Word的字符。
说了这么多应该明白HTTP协议其实只是一段文本,只是文本首行是协议头,一空行之后是协议体,按照这种格式那么浏览器就能解析。
而自定义的协议常见于APP中,因为在APP中定义彼此通信的过程、定义通信中相关字段的含义,即使被抓包后那也很难解析,这种情况很少,因为有现成的协议何必去自己弄一套协议,但是遇到之后应该明白如何下手。
甚至可以开发自己基于socket的爬虫库。
结语
如果你跟我一样都喜欢python,想成为一名优秀的程序员,也在学习python的道路上奔跑,欢迎你加入python学习群:839383765 群内每天都会分享最新业内资料,分享python免费课程,共同交流学习,让学习变(编)成(程)一种习惯!
内容总结
以上是互联网集市为您收集整理的Python爬虫最为核心的HTTP协议解析,及自定义协议的分析!全部内容,希望文章能够帮你解决Python爬虫最为核心的HTTP协议解析,及自定义协议的分析!所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。