requests 简单应用

 

http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  ,官方文档。自己有空看看顺便敲两下熟悉一下。

还有别把文件放的太深。这个idle 识别不了,悲催的很。

 

import requests
response = requests.get('https://www.baidu.com')

print (type(response))
print (response.status_code)
print (type(response.text))
print (response.text)
print (response.cookies)
print (response.content)
print (response.content.decode('utf-8'))

 

发送请求:

  现在,我们有一个名为 r 的 Response 对象。我们可以从这个对象中获取所有我们想要的信息。

import requests

r= requests.get('https://github.com/timeline.json')

Requests 简便的 API 意味着所有 HTTP 请求类型都是显而易见的。例如,你可以这样发送一个 HTTP POST 请求

r = requests.post("http://httpbin.org/post")

r = requests.put("http://httpbin.org/put")
 r = requests.delete("http://httpbin.org/delete")
r = requests.head("http://httpbin.org/get")
r = requests.options("http://httpbin.org/get")


全部摘抄的request的官方文档

你也许经常想为 URL 的查询字符串(query string)传递某种数据。如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。例如,httpbin.org/get?key=val。 Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数。举例来说,如果你想传递 key1=value1 和 key2=value2 到httpbin.org/get ,那么你可以使用如下代码:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)

import requests
data = {
    "name":"zhaofan",
    "age":22
}
response = requests.get("http://httpbin.org/get",params=data)
print(response.url)
print(response.text)

 

posted @ 2018-02-06 17:39  Sakura_柏  阅读(175)  评论(0编辑  收藏  举报