本文介绍了上传文件并处理重定向 [PYTHON]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向网站提交图片https://zxing.org/w/decode.jspx,并阅读结果页面:https://zxing.org/w/decode.

I need to submit an image to the site https://zxing.org/w/decode.jspx, and read the result page : https://zxing.org/w/decode.

我试过了,但它不起作用:

I tried this, but it does not work :

def decode_qrcode(path):
    s = requests.Session()

    url = "https://zxing.org/w/decode.jspx"
    files = {'file': open(path, 'rb')}
    s.post(url, files=files)
    return s.get("https://zxing.org/w/decode").text

我知道有一些图书馆可以读取 QR 码,但我没有找到任何适用于我使用的那种 QR 码(它们可能不支持错误率).

I know that there are librairies to read QR code, but I did not find any that works for the kind of QR codes that I work with (they might have an error rate not supported).

推荐答案

在发出 POST 请求时必须使用 allow_redirects 参数

You have to use the allow_redirects argument when making the POST request

import requests
def decode_qrcode(path):
    s = requests.Session()

    url = "https://zxing.org/w/decode.jspx"
    files = {'file': open(path, 'rb')}
    s.post(url, files=files, allow_redirects = True)
    return s.get("https://zxing.org/w/decode").text

这篇关于上传文件并处理重定向 [PYTHON]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:49