简体   繁体   中英

Flask: 404 Not Found

I am trying to create a REST api using Flask. The problem that I have is, every time i try to visit the route, it returns 404 Not found both in the browser and in the console.

This is my code:

try:
    import urllib.request
    from http.cookiejar import CookieJar
except ImportError:
    import urllib2
    from urllib2 import urlopen
    from cookielib import CookieJar
import re
from flask import Flask, jsonify, request

app = Flask(__name__)
cj = CookieJar()
try:
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
except:
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17')]

@app.route('/imagelookup/<string:img>/', methods=['GET'])
def imageLookUp(img):
    #imagepath = 'http://i.imgur.com/RZPXtOU.jpg'
    #imagepath = 'http://i.imgur.com/eDz2BE5.jpg'
    #imagepath = 'https://image.dhgate.com/0x0s/f2-albu-g6-M01-09-33-rBVaR1qZVlWAdONFAALxr6pjOMU350.jpg/2018-new-arrival-hot-sexy-summer-beach-bikini.jpg'
    imagepath = img

    googlepath = 'http://images.google.com/searchbyimage?image_url='+imagepath

    sourceCode = opener.open(googlepath).read().decode('utf-8')

    regex = r'<input class="gLFyf gsfi"(.*?)>'

    pattern = re.compile(regex)

    findElement = re.findall(pattern, sourceCode)

    for eachElement in findElement:
        validEl = eachElement

    newregex = r'value="(.*?)"'

    newpattern = re.compile(newregex)

    extractName = re.findall(newpattern, validEl)

    return jsonify({'imgObj': extractName})

if __name__ == '__main__':
    app.run(debug=True)

I expect a JSON response but instead, it returns a 404 response code.

This is the URL i tried visiting on my browser: http://127.0.0.1:5000/imagelookup/https://image.dhgate.com/0x0s/f2-albu-g6-M01-09-33-rBVaR1qZVlWAdONFAALxr6pjOMU350.jpg/2018-new-arrival-hot-sexy-summer-beach-bikini.jpg

This is a screenshot of the console, if needed: console

Any help is appreciated. Thanks in advance.

If you want img to be the full url passed after the route prefix, use a type of path rather than string -- see https://flask.palletsprojects.com/en/1.1.x/quickstart/#variable-rules

"Path - like string but also accepts slashes"

So your route should be

@app.route('/imagelookup/<path:img>/', methods=['GET'])
def imageLookUp(img):
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM