标签:www. port header https python pip3 json hub poi
request 模块
#requests 库
#python标准库中提供了: urllib等模块以供http请求,但是,它的api太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。
# /usb/bin/env python
#! -*- encoding=utf=8 -*-
#example:
import urllib.request
#例子1
f = urllib.request.urlopen(‘http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508‘)
result = f.read().decode(‘utf=8‘)
print(result)
#例子2 带请求头
req = urllib.request.Request("http://www.example.com")
req.add_header("Referer","http://www.python.org")
r = urllib.request.urlopen(req)
result = f.read().decode(‘utf-8‘)
print(result)
requests 模块
requests 模块的安装
pip3 install requests
#使用模块
#1 . 无参数实例
import requests
ret = requests.get(‘https://github.com/timeline.json‘)
print(ret.url)
print(ret.text)
#有参数实例
import requests
payload = {‘key1‘:‘value1‘,‘key2‘:‘value2‘}
ret = requests.get("http://httpbin.org/get",params=payload)
print(ret.url)
print(ret.text)
1 post 实例:
import requests
payload = {‘key1‘:‘value1‘,‘key2‘:‘value2‘}
ret = requests.post("http://httpbin.org/post",data=payload)
print(ret.text)
2 发送请求头和数据实例
import requests
import json
url = ‘https://api.github.com/some/endpoint‘
payload = {‘some‘:‘data‘}
headers = {‘content-type‘:‘application/json‘}
ret = requests.post(url,data=json.dumps(payload),headers=headers)
print(ret.text)
print(ret.cookies)
标签:www. port header https python pip3 json hub poi
原文地址:http://www.cnblogs.com/zxcv-/p/7732083.html