爬虫3

HTTP响应状态码参考:

1xx:信息

100 Continue

服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求。

101 Switching Protocols

服务器转换协议:服务器将遵从客户的请求转换到另外一种协议。

2xx:成功

200 OK

请求成功(其后是对GET和POST请求的应答文档)

201 Created

请求被创建完成,同时新的资源被创建。

202 Accepted

供处理的请求已被接受,但是处理未完成。

203 Non-authoritative Information

文档已经正常地返回,但一些应答头可能不正确,因为使用的是文档的拷贝。

204 No Content

没有新文档。浏览器应该继续显示原来的文档。如果用户定期地刷新页面,而Servlet可以确定用户文档足够新,这个状态代码是很有用的。

205 Reset Content

没有新文档。但浏览器应该重置它所显示的内容。用来强制浏览器清除表单输入内容。

206 Partial Content

客户发送了一个带有Range头的GET请求,服务器完成了它。

3xx:重定向

300 Multiple Choices

多重选择。链接列表。用户可以选择某链接到达目的地。最多允许五个地址。

301 Moved Permanently

所请求的页面已经转移至新的url。

302 Moved Temporarily

所请求的页面已经临时转移至新的url。

303 See Other

所请求的页面可在别的url下被找到。

304 Not Modified

未按预期修改文档。客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档)。服务器告诉客户,原来缓冲的文档还可以继续使用。

305 Use Proxy

客户请求的文档应该通过Location头所指明的代理服务器提取。

306 Unused

此代码被用于前一版本。目前已不再使用,但是代码依然被保留。

307 Temporary Redirect

被请求的页面已经临时移至新的url。

4xx:客户端错误

400 Bad Request

服务器未能理解请求。

401 Unauthorized

被请求的页面需要用户名和密码。

401.1

登录失败。

401.2

服务器配置导致登录失败。

401.3

由于ACL 对资源的限制而未获得授权。

401.4

筛选器授权失败。

401.5

ISAPI/CGI应用程序授权失败。

401.7

访问被Web 服务器上的 URL 授权策略拒绝。这个错误代码为 IIS 6.0 所专用。

402 Payment Required

此代码尚无法使用。

403 Forbidden

对被请求页面的访问被禁止。

403.1

执行访问被禁止。

403.2

读访问被禁止。

403.3

写访问被禁止。

403.4

要求SSL。

403.5

要求SSL 128。

403.6

IP地址被拒绝。

403.7

要求客户端证书。

403.8

站点访问被拒绝。

403.9

用户数过多。

403.10

配置无效。

403.11

密码更改。

403.12

拒绝访问映射表。

403.13

客户端证书被吊销。

403.14

拒绝目录列表。

403.15

超出客户端访问许可。

403.16

客户端证书不受信任或无效。

403.17

客户端证书已过期或尚未生效。

403.18

在当前的应用程序池中不能执行所请求的URL。这个错误代码为 IIS 6.0 所专用。

403.19

不能为这个应用程序池中的客户端执行CGI。这个错误代码为 IIS 6.0 所专用。

403.20

Passport登录失败。这个错误代码为 IIS 6.0 所专用。

404 Not Found

服务器无法找到被请求的页面。

404.0

没有找到文件或目录。

404.1

无法在所请求的端口上访问Web 站点。

404.2

Web服务扩展锁定策略阻止本请求。

404.3

MIME映射策略阻止本请求。

405 Method Not Allowed

请求中指定的方法不被允许。

406 Not Acceptable

服务器生成的响应无法被客户端所接受。

407 Proxy Authentication Required

用户必须首先使用代理服务器进行验证,这样请求才会被处理。

408 Request Timeout

请求超出了服务器的等待时间。

409 Conflict

由于冲突,请求无法被完成。

410 Gone

被请求的页面不可用。

411 Length Required

"Content-Length" 未被定义。如果无此内容,服务器不会接受请求。

412 Precondition Failed

请求中的前提条件被服务器评估为失败。

413 Request Entity Too Large

由于所请求的实体的太大,服务器不会接受请求。

414 Request-url Too Long

由于url太长,服务器不会接受请求。当post请求被转换为带有很长的查询信息的get请求时,就会发生这种情况。

415 Unsupported Media Type

由于媒介类型不被支持,服务器不会接受请求。

416 Requested Range Not Satisfiable

服务器不能满足客户在请求中指定的Range头。

417 Expectation Failed

执行失败。

423

锁定的错误。

5xx:服务器错误

500 Internal Server Error

请求未完成。服务器遇到不可预知的情况。

500.12

应用程序正忙于在Web 服务器上重新启动。

500.13

Web服务器太忙。

500.15

不允许直接请求Global.asa。

500.16

UNC授权凭据不正确。这个错误代码为 IIS 6.0 所专用。

500.18

URL授权存储不能打开。这个错误代码为 IIS 6.0 所专用。

500.100

内部ASP 错误。

501 Not Implemented

请求未完成。服务器不支持所请求的功能。

502 Bad Gateway

请求未完成。服务器从上游服务器收到一个无效的响应。

502.1

CGI应用程序超时。 ·

502.2

CGI应用程序出错。

503 Service Unavailable

请求未完成。服务器临时过载或当机。

504 Gateway Timeout

网关超时。

505 HTTP Version Not Supported

服务器不支持请求中指明的HTTP协议版本

(二)爬虫get,post请求区别

当request发送参数为data 时是post     params---get

#基本GET请求(headers参数 和 parmas参数)

get

import requests

infos={

    'kw':input('输入想要查询的信息-')

}

headers = {

    "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"

}

response = requests.get("https://tieba.baidu.com/f?", params = infos, headers = headers)

content = response.content

with open('./baidu.html','wb') as file:

    file.write(content)

(三)利用cookie 访问此网站别的页面时免登陆,不用传参数

import urllib.request

import urllib.parse

import http.cookiejar

data={

    'name': input('用户名:'),

    'pwd': input('密码:')

}

data=urllib.parse.urlencode(data).encode('utf-8')

headers=[

    ('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'),

]

#建cookie对象  COOKIE_jar 是用来存取获得到的cookie

cookie_jar= http.cookiejar.CookieJar()

cookie_handler = urllib.request.HTTPCookieProcessor(cookiejar=cookie_jar)

#创建opener对象

cook_opener=urllib.request.build_opener(cookie_handler)

#在发送请求中增加headers

cook_opener.addheaders=headers

response = cook_opener.open(fullurl='http://192.168.12.18:5678/user/login_handler',data=data)

#可以看一下存取的cookie

print(cookie_jar)

for i in cookie_jar:

    print(i.name,i.value,i.expires)

content = response.read()

print(content)

with open('./renren1.html','wb') as file:

    file.write(content)

#我们利用已经获取到的cookie直接访问另一个页面

response = cook_opener.open(fullurl='http://192.168.12.18:5678/booktest/bookinfo/list')

content = response.read()

print(content)

with open('./renren2.html','wb') as file:

    file.write(content)

我们可以查看一下我们存储的cookie

import requests

url = 'https://www.baidu.com'

headers = {"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"}

response = requests.get(url=url,headers=headers)

cookies = response.cookies

print(type(cookies))

cookies = requests.utils.dict_from_cookiejar(cookies)

print(type(cookies))

print(cookies)

通过记录session 同样也可以

import requests

url1 = 'http://www.renren.com/PLogin.do'

url2 = 'http://www.renren.com/963689126/profile'

data = {

    'email':'18737572516',

    'password':'123456'

}

headers = {

    "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"

}

session=requests.session()

session.headers=headers

response1=session.post(url=url1,data=data)

content1 = response1.content

with open('./renren1.html','wb') as file:

    file.write(content1)

response2 = session.get(url=url2)

content2 = response2.content

with open('./renren2.html','wb') as file:

    file.write(content2)

你可能感兴趣的:(爬虫3)