如何将此curl代码转换为python代码?
curl "https://api.havenondemand.com/1/api/sync/gettextstatistics/v1?url=http%3A%2F%2Fadelaide.backpage.com%2FPetsForSale%2Fcute-english-bulldog-pups-for-adoption%2F19942431&apikey=mykey"
curl -X POST --form "url=http://adelaide.backpage.com/PetsForSale/cute-english-bulldog-pups-for-adoption/19942431" --form "apikey=mykey" https://api.havenondemand.com/1/api/sync/gettextstatistics/v1
答案 0 :(得分:1)
首先可以是偶数
import requests
r = requests.get("https://api.havenondemand.com/1/api/sync/gettextstatistics/v1?url=http%3A%2F%2Fadelaide.backpage.com%2FPetsForSale%2Fcute-english-bulldog-pups-for-adoption%2F19942431&apikey=mykey")
print(r.text)
open("output.txt", "wb").write(r.content)
但更有用的可以是
import requests
url = "https://api.havenondemand.com/1/api/sync/gettextstatistics/v1"
payload = {
"url": "http://adelaide.backpage.com/PetsForSale/cute-english-bulldog-pups-for-adoption/19942431",
"apikey": "mykey",
}
r = requests.get(url, params=payload)
print(r.text)
open("output.txt", "wb").write(r.content)
类似的第二个
import requests
url = "https://api.havenondemand.com/1/api/sync/gettextstatistics/v1"
payload = {
"url": "http://adelaide.backpage.com/PetsForSale/cute-english-bulldog-pups-for-adoption/19942431",
"apikey": "mykey",
}
r = requests.post(url, data=payload)
print(r.text)
open("output.txt", "wb").write(r.content)
get()
使用params=
,post()
使用data=
,但某些API可能会在params=
或data=
中预期get()
和post()
public function ActionSomeController()
{
$notification = new Notifiaction;
$notification->save();
$newJob = (new SendReminderEmail($notification));
$this->dispatch($newJob);
}
。
答案 1 :(得分:0)
如果您可以在系统上安装额外的软件包,我建议使用python requests
模块。