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

2024-04-27 00:10:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有基本的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中编写这段代码的最佳方法是什么,以防此请求将在单独的子进程中使用以减少解析时间?


Tags: comapifalsessldatagetxmlcontent
2条回答

您可以使用requests,from 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)

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

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

相关问题 更多 >