HTTP协议请求

1.http协议请求的7种类型:

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

2.GET请求:

在这里我们实现爬虫自动在百度上查询关键词为hello的结果:

import urllib.request
keywd = "haha"

url = "http://www.baidu.com/s?wd="+keywd
req = urllib.request.Request(url)
data = urllib.request.urlopen(req).read()
fhandle  = open("D:\\pythoncode\\pachong\\sizhou\\httpdemo.html",'wb')
fhandle.write(data)
fhandle.close()

这里需要注意的一个问题是如果查询的是中文则需要进行编码:

import urllib.request
# keywd = "haha"
keywd = "郭畅"
keywd_code = urllib.request.quote(keywd)

url = "http://www.baidu.com/s?wd="+keywd_code
req = urllib.request.Request(url)
data = urllib.request.urlopen(req).read()
fhandle  = open("D:\\pythoncode\\pachong\\sizhou\\httpdemo.html",'wb')
fhandle.write(data)
fhandle.close()

3.post请求:

后期学习一下爬虫自动登陆再详细地的写一下!


猜你喜欢

转载自blog.csdn.net/qq_40276310/article/details/80171908