Python3.x:requests的用法

Python3.x:requests的用法

1,requests 比 urllib.request 容错能力更强;

2,通常用法:

(1).认证、状态码、header、编码、json

r = requests.get(‘https://api.github.com/user‘, auth=(‘user‘, ‘pass‘))
r.status_code

r.headers[‘content-type‘]
输出:‘application/json; charset=utf8‘
r.encoding
输出:‘utf-8‘
r.text
输出:u‘{"type":"User"...‘
r.json()
输出:{u‘private_gists‘: 419, u‘total_private_repos‘: 77, ...}

(2).发起请求

import requests
URL="http://www.bsdmap.com/"
#
r = requests.get(URL)
#
r = requests.post(URL)
#
r = requests.put(URL)
#
r = requests.delete(URL)
#
r = requests.head(URL)
#
r = requests.options(URL)

(3).通过URL传递参数

payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
r = requests.get("http://httpbin.org/get", params=payload)
print r.url
输出:u‘http://httpbin.org/get?key2=value2&key1=value1‘

(4).返回内容

import requests
r = requests.get(‘https://github.com/timeline.json‘)
r.text
输出:‘[{"repository":{"open_issues":0,"url":"https://github.com/...
r.encoding
输出:‘utf-8‘
r.encoding = ‘ISO-8859-1‘

(5).二进制内容

r.content
输出:b‘[{"repository":{"open_issues":0,"url":"https://github.com/...

from PIL import Image
from StringIO import StringIO
i = Image.open(StringIO(r.content))

(6).JSON

import requests
r = requests.get(‘https://github.com/timeline.json‘)
r.json()
输出:[{u‘repository‘: {u‘open_issues‘: 0, u‘url‘: ‘https://github.com/...

(7).超时

requests.get(‘http://github.com‘, timeout=0.001)

(8).自定义header

import json
url = ‘https://api.github.com/some/endpoint‘
payload = {‘some‘: ‘data‘}
headers = {‘content-type‘: ‘application/json‘}

r = requests.post(url, data=json.dumps(payload), headers=headers)

官方文档:http://docs.python-requests.org/en/latest/user/quickstart/

原文地址:https://www.cnblogs.com/lizm166/p/8150854.html

时间: 2024-07-31 14:19:39

Python3.x:requests的用法的相关文章

[实战演练]python3使用requests模块爬取页面内容

本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取响应状态码 5.案例演示 后记 1.安装pip 我的个人桌面系统用的linuxmint,系统默认没有安装pip,考虑到后面安装requests模块使用pip,所以我这里第一步先安装pip. $ sudo apt install python-pip 安装成功,查看PIP版本: $ pip -V 2.

python3 用requests 保存网页以及BeautifulSoup保存图片,并且在本地可以正常显示文章的内容和图片

用requests 模块做了个简单的爬虫小程序,将博客的一篇文章以及图片保存到本地,文章格式存为'.html'.当文章保存到本地后,图片的连接可能是目标站点的绝对或者相对路径,所以要是想在本地也显示图片,需要将保存下来图片的本地路径替换到本地的html文件里. 保存网页用的时requests模块,保存图片用的时BeautifulSoup, 这两个都是第三方模块,需要安装,使用时需要手动导入. **安装方式: pip install requsts 在python3 可能用 pip install

python3添加requests库

1.资源下载 https://codeload.github.com/psf/requests/zip/master https://www.python.org/ https://files.pythonhosted.org/packages/41/b6/4f0cefba47656583217acd6cd797bc2db1fede0d53090fdc28ad2c8e0716/certifi-2018.10.15.tar.gz https://files.pythonhosted.org/pac

python中requests的用法

一个最简单的demo: html = requests.get('http://www.cnblogs.com/liaocheng/p/5215225.html') return html.text 这个函数也可以设置提交参数和表头,当然,也有post版本. 以下为详细: 发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Github的公共时间线 >>&

Python3的requests类抓取中文页面出现乱码的解决办法

这种乱码现象基本上都是编码造成的,我们要转到我们想要的编码,先po一个知识点,嵩天老师在Python网络爬虫与信息提取说到过的:response.encoding是指从HTTP的header中猜测的响应内容编码方式,如果header中不存在charset,则默认编码为ISO-8859-1 ,这样一来某些不规范的服务器返回就必然乱码了:response.apparent_encoding是指从内容中分析出的响应内容编码方式.requests内部的 utils 也提供了一个从返回 body 获取页面

python requests 高级用法 -- 包括SSL 证书错误的解决方案

Session Objects会话对象 Session对象在请求时允许你坚持一定的参数.此外,还坚持由Session实例的所有请求的cookie. 让我们坚持在请求时使用 s = requests.Session() s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') r = s.get("http://httpbin.org/cookies") print r.text # '{"cookies&q

Python3 用 requests 2.x 下载大文件

[环境] OS:Windows 10 x64 Python:3.6.5 x64 requests:2.18.4 [代码] # encoding=utf-8 # author: walker # date: 2018-06-11 # summary: 使用 requests 下载大文件 import time import requests # 下载一个大文件 def DownOneFile(srcUrl, localFile):     print('%s\n --->>>\n  %s'

requests 进阶用法学习(文件上传、cookies设置、代理设置)

一.文件上传 1.模拟网站提交文件 提交此图片,图片名称:timg.jpg import requests files={ 'file':open('timg.jpg','rb') } response=requests.post('http://httpbin.org/post',files=files) print(response.text) { "args": {}, "data": "", "files": { &q

Python3基础语句/符号用法规则(一)

1. 双引号与单引号的用法 ①普通字符串,单双引号都可以用 eg:name = “球球qiu” name = ‘球球qiu’ ②在打英语字符串时,有遇到[’]的单词时需要用双引号来进行区分 eg:word = “let’sgo” ③在需要打[“”]的字符串时,需要用单引号来区分 eg:word = ‘你长的真“好看”’ ④在既有[’]又有[“”]的字符串时,需要用双引号 eg:word = ''' let's go ,you are so "beautiful" ''' ⑤数字不需要加