详解Python requests模块

作者:酒酿小圆子~ 时间:2021-12-31 21:55:12 

前言

虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “HTTP for Humans”,说明使用更简洁方便。

Requests 继承了urllib2的所有特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。

开源地址:https://github.com/kennethreitz/requests

中文文档 API: http://docs.python-requests.org/zh_CN/latest/index.html

一、GET请求

1.1 最基本的GET请求


# 写法一:
response = requests.get("http://www.baidu.com/")
# 写法二:
# response = requests.request("get", http://www.baidu.com/)

1.2 添加headers和查询参数

如果想添加 headers,可以传入headers参数来增加请求头中的headers信息。如果要将参数放在url中传递,可以利用 params 参数。


import requests

kw = {'wd':'长城'}
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"}

# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)

#查看响应内容,response.text 返回的是Unicode格式的数据
print response.text
#<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer> .....

# 查看响应内容,response.content返回的字节流数据
print respones.content

# 查看完整url地址
print response.url
# http://www.baidu.com/?wd=%E9%95%BF%E5%9F%8E

# 查看响应头部字符编码
print response.encoding
# ISO-8859-1

# 查看响应码
print response.status_code
# 200

二、POST请求

2.1 最基本的POST请求


response = requests.post("http://www.baidu.com/", data = data)

2.2 传入data数据

对于 POST 请求来说,我们一般需要为它增加一些参数。那么最基本的传参方法可以利用 data 这个参数。


import requests

formdata = {
   "type":"AUTO",
   "i":"i love python",
   "doctype":"json",
   "xmlVersion":"1.8",
   "keyfrom":"fanyi.web",
   "ue":"UTF-8",
   "action":"FY_BY_ENTER",
   "typoResult":"true"
}

url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"

headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}

response = requests.post(url, data = formdata, headers = headers)

print response.text
# {"type":"EN2ZH_CN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"i love python","tgt":"我喜欢python"}]],"smartResult":{"type":1,"entries":["","肆文","高德纳"]}}

# 如果是json文件可以直接显示
print response.json()
# {u'errorCode': 0, u'elapsedTime': 0, u'translateResult': [[{u'src': u'i love python', u'tgt': u'\u6211\u559c\u6b22python'}]], u'smartResult': {u'type': 1, u'entries': [u'', u'\u8086\u6587', u'\u9ad8\u5fb7\u7eb3']}, u'type': u'EN2ZH_CN'}

来源:https://blog.csdn.net/u012856866/article/details/117929358

标签:Python,requests,模块
0
投稿

猜你喜欢

  • 人脸检测实战终极之OpenCV+Python实现人脸对齐

    2023-10-01 02:03:07
  • Python终端输出彩色字符方法详解

    2023-11-18 22:04:17
  • HTML邮件的又一点思考

    2009-05-06 13:33:00
  • python异步Web框架sanic的实现

    2021-01-17 01:37:57
  • python十进制转二进制的详解

    2023-06-07 23:39:33
  • Django中自定义模型管理器(Manager)及方法

    2022-12-01 17:53:05
  • Python字符串三种格式化输出

    2022-12-15 09:47:59
  • Python计算素数个数的两种方法

    2023-09-09 16:38:19
  • Python API 操作Hadoop hdfs详解

    2023-02-24 02:23:26
  • php+mysqli实现批量替换数据库表前缀的方法

    2023-11-22 10:15:55
  • ASP开发中可能遇到的错误信息中文说明大全(整理收集)第1/2页

    2010-07-02 09:50:31
  • python使用Word2Vec进行情感分析解析

    2023-10-29 21:16:47
  • 十六则Dreamweaver使用快技法

    2009-07-05 18:55:00
  • python 协程并发数控制

    2023-09-20 01:07:10
  • PHP适配器模式Adapter Pattern的使用介绍

    2023-06-10 12:28:21
  • 如何Restore数据库备份文件?

    2009-11-02 20:20:00
  • 最大限度优化你的Asp性能

    2007-10-01 18:04:00
  • php截取字符串函数分享

    2023-11-14 10:53:21
  • Python读取一个目录下所有目录和文件的方法

    2023-05-30 23:04:21
  • 分析用Python脚本关闭文件操作的机制

    2021-01-25 07:03:26
  • asp之家 网络编程 m.aspxhome.com