1. get请求,传递url参数,传递请求zhi
import requests 导入requests模块
base_url='http://httpbin.org/' 设置请求网址
para_data={'user':'51zxw','passwd':'6666'} 将要传递的参数以字典格式传递。赋值给para-data
r=requests.get(base_url+'get',params=para_data) 进行get请求,请求的url。传递的参数复制给params(库中定义好的)。
print(r.url) 打印请求的url和参数
print(r.status_code) 打印返回的状态码。
2.传递body参数, post请求,一般参数都是在请求体中传递,所以可以刑警body参数传递。
import requests
base_url='http://httpbin.org/'
para_data={'user':'51zxw','passwd':'6666'}
r=requests.post(base_url+'post',params=para_data) 采用post方法,方法和get类似。
print(r.text) 打印相应体。
3. 传递请求体,请求体定制。库中定制了headers,为请求头数据。设定请求头为火狐。
如果给知乎进行爬虫,因为知乎含有反爬虫机制,所以需要添加请求头,请求头数据直复制浏览器中内容。
print(r.json),将返回结果以json样式返回,
1. get请求,传递url参数,传递请求zhiimport requests 导入requests模块base_url='http://httpbin.org/' ...
>>> import requests
>>> res = requests.head(http://www.baidu.com/)
>>> req.head(https://www.baidu.com/).headers
{'Content-Encoding': 'gzip', 'Server': 'bfe/1.0.8.18', 'Last-Modified': 'Mon, 13 Jun 2016 02:50:08 GMT', 'Connect
#发送网络请求
response = requests.get('http://httpbin.org/get?name=Jack&age=30')
print(response.text)
输出结果:
"args": {
"age": "30",
"name": "Jack"
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
Java的世界里,HttpClient 是一个功能强大的Http请求
库
,然而接口非常复杂,设计上遵从正交性,简单的请求也需要写比较多的代码,更不要说隐藏在各种细节里面的高级用法了。Requests, 是一个模仿
python
requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。
简单的请求示例:String
url
= ...;
Response resp = Requests.get(
url
).text();
// post 和其他方法
resp = Requests.post(
url
).text();
resp = Requests.head(
url
).text();
//读取Http Response
int statusCode = resp.getStatusCode();
Headers headers = resp.getHeaders();
Cookies cookies = resp.getCookies();
String
body
= resp.get
Body
();
//response 返回其他类型
resp = Requests.get(
url
).text("UTF-8");
// get response as bytes
Response resp1 = Requests.get(
url
).bytes();
// save response as file
Response resp2 = Requests.get(
url
).file("/path/to/save/file");
//
url
参数
:
Map map = new HashMap();
map.put("k1", "v1");
map.put("k2", "v2");
Response resp = Requests.get(
url
).param("key1", "value1").params(map)
//.params(new Parameter(...), new Parameter(...))
.text();
//
请求头
Response resp = Requests.get(
url
).header("key1", "value1").headers(map)
//.headers(new Header(...), new Header(...))
.text();
// 添加Cookie:
Map cookies = new HashMap();
Response resp = Requests.get(
url
).cookie("key1", "value1").cookies(map)
//.cookies(new Cookie(...), new Cookie(...))
.text();
// 设置 userAgent
Response resp = Requests.get(
url
).userAgent(userAgent).text();
// 增加请求数据(post, put, patch方法)
// send form-encoded data. x-www-form-
url
encoded header will be send automatically
Response resp = Requests.post(
url
).data(map).text();
// send string data
String str = ...;
resp = Requests.post(
url
).data(str, "UTF-8").text();
// send from inputStream
InputStream in = ...
resp = Requests.post(
url
).data(in).text();
// multipart 请求, 用于文件上传:
Response resp = Requests.post(
url
).data(map).multiPart("ufile", "/path/to/file")
.multiPart(..., ...).text();请求设置://禁止自动重定向
之前在osc看到一个文章讨论Get和Post的不同, 有人说不能用Get来上传文件。这就是用Get上传文件的例子,client用来发
Get请求
,server用来收请求。文件内容是在http请求的
body
内传过去的。用了不同的语言,因为我觉得各自处理起来都要方便些。而且我觉得浏览器也是可以发出这样的请求的,之后我会尝试一下。
请求端代码
复制代码 代码如下:
import requests #需要安装requests
with open(‘test.txt’, ‘rb’) as f:
requests.get(‘http://127.0.0.1:9999’, data=f)
服务端代码
url
= 'http://www.baidu.com' # 使用?携带
参数
r = requests.get(
url
)
#简单的
get请求
用request.get()而需要自定义
请求头
的get用request.Request()
#简单的
get请求
只能把数据放在
url
里
print(r.text)#text:获得借口返回值的文本格式
1、
url
格式:http://接口地址?ke
c := new(http.Client)
req := request.NewRequest(c)
resp, err := req.Get("http://httpbin.org/get")
j, err := resp.Json()
defer resp.
Body
.Close() // Don't forget close the response
body
POST:
req.Data = map[string]string{
"key": "value",
"a": "123",
resp, err := req.Post("http://httpbin.org/post")
Cookies:
req.Cookies = map[string]string{
"key": "value",
"a": "123",
resp, err := req.Get("http://httpbin.org/cookies")
Headers:
req.Headers = map[string]string{
"Accept-Encoding": "gzip,deflate,sdch",
"Accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
resp, err := req.Get("http://httpbin.org/get")
Files:
f, err := os.Open("test.txt")
req.Files = []request.FileField{
request.FileField{"file", "test.txt", f},
}resp, err := req.Post("http://httpbin.org/post")
Json:
req.Json = map[string]string{ "a": "A", "b": "B",
}resp, err := req.Post("http://httpbin.org/post")
req.Json = []int{1, 2, 3}
resp, err = req.Post("http://httpbin.org/post")
Proxy:
req.Proxy = "http://127.0.0.1:8080"
// req.Proxy = "https://127.0.0.1:8080"
// req.Proxy = "socks5://127.0.0.1:57341"
resp, err := req.Get("http://httpbin.org/get")
or https://github.com/mozillazg/request/tree/develop/_example/proxy
HTTP Basic Authentication:
req.BasicAuth = request.BasicAuth{"user", "passwd"}
resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")
这是一个
GET请求
的示例:
require "requests"
response = Requests . request ( "GET" , "http://example.com" )
# Now you have these methods available
response . status #=> Number with the status code
response . headers #=> Hash with the response headers
response .
body
#=> String with the response
body
如果您不是直接调用Requests.request而是直接指定HTTP方法,
res = requests.get(
url
, verify=False, headers=headers) html = res.text bs = BeautifulSoup(html, "html.parser") t
body
= bs.find("
body
") job_list = t
body
.findAll(name='div', attrs={"class": "job_list2"})