python中的requests使用小结

现接触到的很少,详细的官方教程地址:

requests官方指南文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
requests高级指南文档:http://docs.python-requests.org/zh_CN/latest/user/advanced.html#advanced

1.安装request,bs4

pip install requests
pip install bs4

2.代码文档中调用

import requests
from bs4 import BeautifulSoup

3.发送http请求

r = requests.get(‘https://github.com/timeline.json’) #GET请求
r = requests.post(“http://httpbin.org/post”) #POST请求
r = requests.put(“http://httpbin.org/put”) #PUT请求
r = requests.delete(“http://httpbin.org/delete”) #DELETE请求
r = requests.head(“http://httpbin.org/get”) #HEAD请求
r = requests.options(“http://httpbin.org/get”) #OPTIONS请求

4.get方法

r = requests.get(‘http://dict.baidu.com/s‘, params={‘wd‘:‘python‘})  #带参数的GET请求
#等于http://dict.baidu.com/s?wd=python
r = requests.get(URL, params={‘ip‘: ‘8.8.8.8‘}, timeout=1) #设置超时时间
r = requests.get(‘https://httpbin.org/hidden-basic-auth/user/passwd‘, auth=HTTPBasicAuth(‘user‘, ‘passwd‘))  #基本身份认证(HTTP Basic Auth)
r = requests.get(URL, auth=HTTPDigestAuth(‘user‘, ‘pass‘)) #摘要式身份认证(HTTP Digest Auth)
cookies = {‘testCookies_1‘: ‘Hello_Python3‘, ‘testCookies_2‘: ‘Hello_Requests‘}
r = requests.get(url, cookies=cookies)
#带cookies的请求
r = requests.get(‘http://www.baidu.com/link‘, allow_redirects = False) #禁止URL跳转
proxies = {
  "http": "http://10.10.1.10:3128",
  "http": "http://user:[email protected]:3128/",
  "https": "http://10.10.1.10:1080",
}
r = requests.get("http://www.baidu.com", proxies=proxies) #代理访问
headers = {‘Accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,
           ‘Accept-Encoding‘: ‘gzip, deflate, compress‘,
           ‘Accept-Language‘: ‘en-us;q=0.5,en;q=0.3‘,
           ‘Cache-Control‘: ‘max-age=0‘,
           ‘Connection‘: ‘keep-alive‘,
           ‘User-Agent‘: ‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0‘}
r = requests.get(‘http://www.baidu.com‘, headers = headers)
#自定义head请求页面

5.POST方法

files = {‘file‘: open(‘/home/1.mpg‘, ‘rb‘)}
r = requests.post(url, files=files)
#POST上传文件
data = {‘some‘: ‘data‘}
headers = {‘content-type‘: ‘application/json‘,
           ‘User-Agent‘: ‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0‘}
r = requests.post(‘https://www.baidu.com‘, data=data, headers=headers)
#POST自定义HEAD提交数据
r = requests.post(‘https://www.baidu.com‘, data=json.dumps({‘some‘: ‘data‘}))
#POST提交JSON数据
时间: 2024-08-05 02:53:12

python中的requests使用小结的相关文章

Python中使用requests和parsel爬取喜马拉雅电台音频

场景 喜马拉雅电台: https://www.ximalaya.com/ 找到一步小说音频,这里以下面为例 https://www.ximalaya.com/youshengshu/16411402/ 博客:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸道的程序猿获取编程相关电子书.教程推送与免费下载. 实现 找到下载地址 使用谷歌浏览器打开上面网址,按F12打开调试,点击播放按钮后,然后找到Network下的Media下的Headers下的Req

python中的random模块小结

目录 python中的random模块总结 调用 random.random() random.uniform(a, b) random.randint(a, b) random.randrange([start=0], stop, [step=1]) random.choice(sequence) random.shuffle(x, [random]) random.sample(sequence, k) python中的random模块总结 调用 import random # python

python中的变量引用小结

python的变量都可以看成是内存中某个对象的引用.(变量指向该内存地址存储的值) 1.python中的可更改对象和不可更改对象 python中的对象可以分为可更改(mutable)对象与不可更改(immutable)对象 strings, tuples, 和numbers是不可更改的对象 list,dict等则是可以修改的对象. 如果某个变量指向不可更改的对象,则重新赋值时.原对象被抛弃,变量会指向一个新对象. 若指向的是可更改的对象,则重新赋值时,会也会重新赋值内存中对象的值. 如图:nfo

python中的BeautifulSoup使用小结

1.安装 pip install beautifulsoup4 2.代码文件中导入 from bs4 import BeautifulSoup 3. 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup, "html.parser") Python的内置标准库 执行速度适中 文档容错能力强 Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差 lxml HTML 解析器 BeautifulSoup(markup, "lx

Python中的requests模块注意事项

主要是说requests.post()方法, 参数: url :  这就不解释了 data:  如果传入的是字典类型,则字典在发出请求时会自动编码为表单形式,表单形式会将字典中的键和值进行一些操作: key1=value1&key2=value2 如果传入的是字符串类型,则数据会被直接发送出去. 2.4.2版本开始提供了json参数,默认会执行json.dumps() headers 我们可以自定义请求头部. content-type: application/json   applicatio

python中安装requests后又提示错误

刚刚我们是安装成功了的,新建一个项目又提示红色的波浪线了,,郁闷了 解决方法:点击pycharm菜单:File-Settings,键入Project Interpreter,我电脑python安装路径是E:\python\python.exe,而红框内显示的是python工程目录,必须要修改成python正确的安装路径 原文地址:https://www.cnblogs.com/jpr-ok/p/9168250.html

python中的try/except/else/finally语句--自我小结

def main():      try:        print "try"        return True    except:        print "except"    finally:        print "finally"        print "main" 输出: try finally 就算try中有return语句,最终的finally也会被执行. 其他人的一些好的总结: http:/

python中的字典内置方法小结

#!/usr/local/bin/python3 # -*- coding:utf-8 -*- #key-value #dict 无序,无下标,不需要下标,因为有key stu={ 'stu001':"zhang yu", 'stu002':"ma hong yan", 'stu003':"zhang guo bin", 'stu004':"sha chun hua" } ''' -----------------------

快速入门Python中文件读写IO是如何来操作外部数据的?

读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中读取数据(读文件),或者把数据写入这个文件对象(写文件). 读文件 要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 >>> f =