如何在python上编写curl get请求以减少解析时间

时间:2014-06-26 06:20:51

标签: php python curl

我有基本的curl GET请求在php中使用网站API:

$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中写这个的最好方法是什么,这个请求将在单独的子进程中使用以减少解析时间?

2 个答案:

答案 0 :(得分:3)

您可以使用以下任何一个模块:

  • 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
.
.

答案 1 :(得分:3)

您可以使用requests来自请求文档;

>>> 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)