将CURL调用转换为Python脚本
发现将Windows中的简单CURL请求转换为Python脚本有问题 CURL命令是将CURL调用转换为Python脚本,python,curl,Python,Curl,发现将Windows中的简单CURL请求转换为Python脚本有问题 CURL命令是 from urllib.parse import urlencode from urllib.request import Request, urlopen url = 'http://192.168.0.106:8080/parser' # Set destination URL here post_fields = {"query": "NEW YORK"} # Set POST fields h
from urllib.parse import urlencode
from urllib.request import Request, urlopen
url = 'http://192.168.0.106:8080/parser' # Set destination URL here
post_fields = {"query": "NEW YORK"} # Set POST fields here
request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)
curl-X POST-d“{\'query\':\'newyork\'”http://192.168.0.106:8080/parser
我得到输出:
[{"label":"state","value":"new york"}]
Python脚本是
from urllib.parse import urlencode
from urllib.request import Request, urlopen
url = 'http://192.168.0.106:8080/parser' # Set destination URL here
post_fields = {"query": "NEW YORK"} # Set POST fields here
request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)
输出为[]。基本上没什么。只需使用
请求即可:
import requests
data = '{"query": "NEW YORK"}'
response = requests.post('http://192.168.0.106:8080/parser', data=data)
完整文档可在中找到。您在cURL请求中创建的示例查询是否也这样做?很抱歉,在这两种情况下都是纽约。我已经编辑了这个问题。谢谢你的回答。你能告诉我为什么我的程序不起作用吗?@ZaidKhan不太确定。我实际上不使用urllib
。我刚刚将您的curlpost请求转换为请求
,这是在python中最简单的方法。