自定义标头
发送你自己指定的标头,包括 cookie
你可以通过使用 headers 参数并在字典中指定标头来传递自定义标头,其中每个键代表标头名称,其对应的值代表标头值
若要在请求中使用 cookie,你可以传递一个字典数组,其中每个字典代表一个带有“键”和“值”键值对的 cookie
以下是使用 2 个自定义 cookie 以及 2 个标头的代码示例,所有这些都可以自定义:
curl -X POST -u username:password -H 'Content-Type: application/json' -d '{"target": "universal", "url": "http://httpbin.org/headers", "geo": "United States", "headless": "html", "cookies": [{"key": "my_cookie", "value": "delicious"}, {"key": "second_cookie", "value": "delisheshner"}], "headers": {"Accept-Language": "en-US", "My-Header": "whatever you can imagine"}}' https://scraper-api.smartdaili-china.com/v2/tasks
import requests
payload = {
'target': 'universal',
'url': 'http://httpbin.org/headers',
'geo': 'United States',
'headless': 'html',
'cookies': [{'key': 'my_cookie', 'value': 'delicious'}, {'key': 'another_cookie', 'value': 'delisheshner'}],
'headers': {'Accept-Language': 'en-US', 'My-Header': 'whatever you can imagine'}
}
response = requests.request(
'POST',
'https://scraper-api.smartdaili-china.com/v1/tasks',
auth=('username', 'password'),
json=payload,
)
print(response.text)
<?php
$payload = array(
'target' => 'universal',
'url' => 'http://httpbin.org/headers',
'headless' => 'html',
'cookies' => array(array('key' => 'my_cookie', 'value' => 'delicious'), array('key' => 'second_cookie', 'value' => 'delisheshner')),
'headers' => array('Accept-Language' => 'en-US', 'My-Header' => 'whatever you can imagine')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://scraper-api.smartdaili-china.com/v1/tasks');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
Updated about 1 month ago