如何在 Python 上通过 post 或 get 方法使用 API QR Monkey
我正在尝试使用免费 api https://www.qrcode-monkey.com 并且我在任何地方都找不到 python 的有效示例,我想我是按照文档进行操作的。我正在做一些试验,在 POST 上我继续收到方法错误,在 GET 上我收到很多 400 错误...
这是两者的代码,任何人都知道我做错了什么?谢谢你!
import requests
from urllib.parse import quote, urlencode
class QrManager:
def __init__(self):
self.url = "https://qrcode-monkey.com/"
def get_data_post(self):
url = self.url + "qr/custom"
payload = {
"data": "https://www.google.com",
"config": {
"body": "circle",
},
"size": 300,
"download": False,
"file": "png"
}
req = requests.post(url, json=payload)
return req
def get_data_get(self):
template_url = self.url + "qr/custom/?{}"
params = {
"data": "https://www.google.com",
"config": {
"body": "circle",
},
"size": 300,
"download": False,
"file": "png"
}
url = template_url.format(urlencode(params, safe="()", quote_via=quote))
req = requests.get(url)
return req
qrm = QrManager()
# response = dm.get_data_post()
response = qrm.get_data_get()
print(response.status_code)
print(response.url)
print(response.text)
I'm trying to use the free api https://www.qrcode-monkey.com and I can't find anywhere a valid example for python, I think I followed thru the documentation. I'm doing some trial and on POST I continue getting method errors and on GET I get a lot of 400 errors...
Here is the code with both, anyone knows what I'm doing wrong? Thank you!
import requests
from urllib.parse import quote, urlencode
class QrManager:
def __init__(self):
self.url = "https://qrcode-monkey.com/"
def get_data_post(self):
url = self.url + "qr/custom"
payload = {
"data": "https://www.google.com",
"config": {
"body": "circle",
},
"size": 300,
"download": False,
"file": "png"
}
req = requests.post(url, json=payload)
return req
def get_data_get(self):
template_url = self.url + "qr/custom/?{}"
params = {
"data": "https://www.google.com",
"config": {
"body": "circle",
},
"size": 300,
"download": False,
"file": "png"
}
url = template_url.format(urlencode(params, safe="()", quote_via=quote))
req = requests.get(url)
return req
qrm = QrManager()
# response = dm.get_data_post()
response = qrm.get_data_get()
print(response.status_code)
print(response.url)
print(response.text)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们没有在文档中显示它,但它需要不同的 URL - 使用
api.
而不是www。
我在 Firefox/Chrome 中使用了
DevTools
( tab:Network
) 查看页面生成 QR 时使用的 url。还有其他问题。
POST
给出带有圆形
的QR
,但GET
给出普通正方形
。GET
需要将config
转换为json
才能获取circles
(但不需要
urlencode< /code>)
完整代码。
结果:
They didn't show it in documentation but it needs different URL - with
api.
instead ofwww.
I used
DevTools
in Firefox/Chrome (tab:Network
) to see url used when page generates QR.There is other problem.
POST
givesQR
withcircles
butGET
gives with normalsquares
.GET
needs to convertconfig
tojson
to getcircles
(but it doesn't need
urlencode
)Full code.
Result: