requests库的基本使用

jacklove
发布: 2018-06-11 22:55:05
原创
3157人浏览过

1. response.content和response.text的区别

response.content是编码后的byte类型(“str”数据类型),response.text是unicode类型。这两种方法的使用要视情况而定。注意:unicode -> str 是编码过程(encode()); str -> unicode 是解码过程(decode())。示例如下:

# --coding:utf-8-- #
import requests
response = requests.get("https://baidu.com/")
print response.url
print type(response.content)
with open("C:\Users\Administrator\Desktop\content.html", "w") as f:
    f.write(response.content)
    print "content保存成功"
print type(response.text)
with open("C:\Users\Administrator\Desktop\text.html", "w") as f:
    # 返回url的编码方式
    print response.encoding
    f.write(response.text.encode("ISO-8859-1"))
    print "text保存成功"
登录后复制

2. 发送get请求,直接调用“resquests.get" 就可以了。response的一些属性:response.text; response.content; response.url; response.encoding; response.status_code

# --coding:utf-8-- #
import requests
params = {
    "wd": "中国"
}
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
response = requests.get("https://baidu.com/s", params=params, headers=headers)
print response.url
with open("C:\Users\Administrator\Desktop\get.html", "w") as f:
    f.write(response.content)
    print "保存成功"
登录后复制

3. 发送post请求:传入data信息。注意get请求传入的是params信息。示例如下:

# --coding:utf-8-- #
import requests
data = {
    "first": "true",
    "pn": "1",
    "wd": "python"
}
headers = {
    "Referer": "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
response = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", data=data, headers=headers)
print response.encoding
print type(response.content)
with open("C:\Users\Administrator\Desktop\post.html", "w") as f:
    f.write(response.content)
    print "保存成功"
登录后复制

4. 使用代理。在get方法中增加proxy参数即可。示例代码如下:

# --coding:utf-8-- #
import requests
proxy = {
    "http": "124.42.7.103"
}
response = requests.get("http://httpbin.org/ip", proxies=proxy)
print response.content
登录后复制

5. requests处理cookies信息。使用requests.Session()方法即可。示例代码如下:

# --coding:utf-8-- #
import requests
url = "http://www.renren.com/PLogin.do"
# url = "http://www.renren.com/SysHome.do"
data = {"email": "账号", "password": "密码"}
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
session = requests.Session()
session.post(url, data=data, headers=headers)
response = session.get("http://www.renren.com/543484094/profile")
with open("C:\Users\Administrator\Desktop\Liwei.html", "w") as fp:
    fp.write(response.content)
    print "保存成功"
登录后复制

6. 处理不信任的SSL证书。与上面的代码相比,多了一个verify=False参数,为了处理SSL证书不受信用的问题。

示例代码如下:

response = session.get("http://www.renren.com/543484094/profile", verify=False)
登录后复制

以上就是关于requests库的基本使用。

本文讲解了requests库的基本使用 ,更多相关内容请关注php中文网。

相关推荐:

前端调用微信支付接口

jQuery对象与DOM对象

jQuery插件开发标准写法

以上就是requests库的基本使用的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号