使用Python爬虫库requests发送表单数据和JSON数据
人气:0导入Python爬虫库Requests
import requests
一、发送表单数据
要发送表单数据,只需要将一个字典传递给参数data
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload) print(r.text)
{"args":{},"data":"","files":{},"form":{"key1":"value1","key2":"value2"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"23","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"python-requests/2.14.2"},"json":null,"origin":"110.18.198.88","url":"http://httpbin.org/post"}
也可以将一个元组列表传递给参数data,这样可以实现多个value对应一个key的情况(字典的key必须唯一)
payload = (('key1', 'value1'), ('key1', 'value2')) r = requests.post('http://httpbin.org/post', data=payload) print(r.text)
{"args":{},"data":"","files":{},"form":{"key1":["value1","value2"]},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"23","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"python-requests/2.14.2"},"json":null,"origin":"110.18.198.88","url":"http://httpbin.org/post"}
二、发送字符串(JSON)数据
将json字符串传递给参数data
import json url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} r = requests.post(url, data=json.dumps(payload))
将字典传递给参数json
url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} r = requests.post(url,json=payload)
更多关于Python爬虫库requests的使用方法请查看下面的相关链接
您可能感兴趣的文章:
- python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
- python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
- python爬虫库scrapy简单使用实例详解
- 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
- Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
- Python爬虫库BeautifulSoup的介绍与简单使用实例
- Python爬虫库requests获取响应内容、响应状态码、响应头
- 使用Python爬虫库requests发送请求、传递URL参数、定制headers
- 常用python爬虫库介绍与简要说明
- python3第三方爬虫库BeautifulSoup4安装教程
- 小众实用的Python 爬虫库RoboBrowser
加载全部内容