Reptile - Reptile know

Reptile - Reptile know

What is the Internet? What reptiles do is?

  • What we call the Internet is sending a request by a client computer to the target computer to download the data to the target computer's local process.

  • Users to access network data is: the browser to submit a request -> download page code -> parsing / rendering into pages.

  • The crawlers have to do is: Analog browser sends a request -> download page code -> only to extract useful data -> stored in the database or file

definition

By writing a program to simulate the Internet browser to initiate a request to the site, let it analyze and extract the basic flow of the program reptiles useful data after obtaining data on to the Internet

"""
1、发起请求
使用http库向目标站点发起请求,即发送一个Request Request包含:请求头、请求体等

2、获取响应内容
如果服务器能正常响应,则会得到一个Response Response包含:html,json,图片,视频等

3、解析内容
解析html数据:正则表达式,第三方解析库如Beautifulsoup,pyquery等 
解析json数据:json模块 
解析二进制数据:以b的方式写入文件

4、保存数据
数据库 文件
"""

Request and response

http protocol: https://home.cnblogs.com/u/waller/
Request: the user's own information sent by the browser (socket client) to the server (socket server)

The Response: The server receives the request, analyzing the request information sent by a user, and return data (the data returned may contain other links, such as: images, JS, CSS, etc.)

ps: browser after receiving Response, parses its content to display to the user, while the analog crawlers and the browser sends a request receiving Response, to extract useful data therein.

Request

1.请求方式:
    常用的请求方式:GET,POST
    其他请求方式:HEAD,PUT,DELETE,OPTHONS
'''
post与get请求最终都会拼接成这种形式:k1=xxx&k2=yyy&k3=zzz
post请求的参数放在请求体内:
    可用浏览器查看,存放于form data内
get请求的参数直接放在url后
'''
2.请求的url
    url全称统一资源定位符,如一个网页文档,一张图片
    一个视频等都可以用url唯一来确定
'''
url编码
    https://www.baidu.com/s?wd=图片
    图片会被编码(看示例代码)
'''
'''
网页的加载过程是:
加载一个网页,通常都是先加载document文档,
在解析document文档的时候,遇到链接,则针对超链接发起下载图片的请求
'''
3、请求头
    User-Agent:请求头中如果没有user-agent客户端配置,
    服务端可能将你当做一个非法用户
    host
    cookies:cookie用来保存登录信息
'''
一般做爬虫都会加上请求头
'''
4.请求体
    如果是get方式,请求体没有内容
    如果是post方式,请求体是format data
'''
ps:
1、登录窗口,文件上传等,信息都会被附加到请求体内
2、登录,输入错误的用户名密码,然后提交,就可以看到post,正确登录后页面通常会跳转,无法捕捉到post 
'''

Response

1、响应状态
    200:代表成功
    301:代表跳转
    404:文件不存在
    403:权限
    502:服务器错误
2、Respone header
    set-cookie:可能有多个,是来告诉浏览器,把cookie保存下来
3、preview就是网页源代码
    最主要的部分,包含了请求资源的内容
    如网页html,图片
    二进制数据等

Reptile classification

  • 1 General Reptile: get a whole page of data
  • 2 focused crawler: Gets the data within the specified page
  • 3 Incremental reptiles: to detect updates website data website crawling out of the latest update data

"Attack" and "defense"

"Attack": anti-climb mechanism

网站采用相关的技术手段或策略阻止爬虫程序对网站数据的爬取
- UA检测: 服务器通过对请求中的请求头里的UA的值,来判定请求载体的身份
# robots协议  (君子协议)
通常用在门户网站中,声明了哪些数据可爬,哪些数据不可爬
eg:
    https://www.taobao.com/robots.txt

"Defense": anti-anti-climb policy

让爬虫程序通过破解反爬机制获取数据
# ps: 网站不同策略不同,需要分析
一般使用到的头信息:
    请求头
    - User-Agent : 请求载体的身份标识
        载体: 浏览器, 爬虫程序
    - Connection : keep-alive | close
        close : 当用爬虫请求成功后,这个请求对应的连接会立刻断开
    响应头
    - content-type : 数据响应方式, 服务器按对应方式响应数据

supplement

http协议: client与server进行数据交互的一种规定形式
https协议: 安全的http协议,在http基础之上加了一个安全层的操作(ssl加密)
    - 对称秘钥加密:(不安全)
        加密数据级密规则(私钥)发送到服务端破解
    - 非对称秘钥加密:(效率低, 不安全:客户端拿到的公钥不一定是服务端的)
        服务器端制定加密方式(公钥)和解密方式(私钥),客户端获取公钥加密数据再传输
    - 证书秘钥加密: 
        公钥 -> 证书机构 -> 添加证书 -> 客户端 -> 加密数据 -> 服务端

ps: just reptile tool, neither good nor bad

Guess you like

Origin www.cnblogs.com/wwbplus/p/12122099.html