我有基本的curl GET请求与站点API在php工作:
$headers = array(
"Content-type: text/xml;charset='"windows-1251'"",
"Host:api.content.com",
"Accept:*/*",
"Authorization:qwerty"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.content.com/v1.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$f = new SimpleXMLElement($data);
#echo $data;
$total = $f['total'];
curl_close($ch);
}
如果这个请求将在单独的子进程中使用以减少解析时间,那么在python中编写这个请求的最佳方法是什么? 你可以使用request, from requests document;
>>> 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)
您可以使用以下任何模块:
-
urllib2
(默认为python) -
requests
(需要安装)
的例子:
>>> import requests
>>> r = requests.get('http://example.com/')
>>> print r.text
.
.
>>> import urllib2
>>> response = urllib2.urlopen('http://example.com')
>>> print response.info()
.
.
>>> html = response.read()
>>> print html
.
.