Mar*_*ill 120
我会试试请求库.基本上只是一个更容易使用的标准库模块(即urllib2,httplib2等)的包装器,你将用于相同的事情.例如,从需要基本身份验证的URL获取json数据将如下所示:
import requests
response = requests.get('http://thedataishere.com',
auth=('user', 'password'))
data = response.json()
Run Code Online (Sandbox Code Playgroud)
对于kerberos身份验证,请求项目具有reqests-kerberos库,该库提供可用于请求的kerberos身份验证类:
import requests
from requests_kerberos import HTTPKerberosAuth
response = requests.get('http://thedataishere.com',
auth=HTTPKerberosAuth())
data = response.json()
Run Code Online (Sandbox Code Playgroud)
Tru*_*ufa 77
除非我忽略了这一点,否则这样的事情应该有效:
import json
import urllib2
json.load(urllib2.urlopen("url"))
Run Code Online (Sandbox Code Playgroud)
小智 27
您基本上需要向服务发出HTTP请求,然后解析响应的主体.我喜欢使用httplib2:
import httplib2 as http
import json
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
uri = 'http://yourservice.com'
path = '/path/to/resource/'
target = urlparse(uri+path)
method = 'GET'
body = ''
h = http.Http()
# If you need authentication some example:
if auth:
h.add_credentials(auth.user, auth.password)
response, content = h.request(
target.geturl(),
method,
body,
headers)
# assume that content is a json reply
# parse content with the json module
data = json.loads(content)
Run Code Online (Sandbox Code Playgroud)
如果您希望使用Python 3,可以使用以下内容:
import json
import urllib.request
req = urllib.request.Request('url')
with urllib.request.urlopen(req) as response:
result = json.loads(response.readall().decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
162404 次 |
最近记录: |