Python无法向axios请求发送数据

2024-03-29 01:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我左右为难。当我从邮递员那里发送请求时,我得到服务器状态200和所需的数据。使用Axios时,服务器状态为500内部服务器错误和日志中的错误:

2020-08-17T09:25:28.361222+00:00 app[web.1]: [2020-08-17 09:25:28,359] ERROR in app: Exception on / [POST]
2020-08-17T09:25:28.361223+00:00 app[web.1]: Traceback (most recent call last):
2020-08-17T09:25:28.361224+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
2020-08-17T09:25:28.361224+00:00 app[web.1]:     response = self.full_dispatch_request()
2020-08-17T09:25:28.361224+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
2020-08-17T09:25:28.361225+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2020-08-17T09:25:28.361225+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
2020-08-17T09:25:28.361226+00:00 app[web.1]:     return cors_after_request(app.make_response(f(*args, **kwargs)))
2020-08-17T09:25:28.361226+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
2020-08-17T09:25:28.361226+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2020-08-17T09:25:28.361227+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2020-08-17T09:25:28.361228+00:00 app[web.1]:     raise value
2020-08-17T09:25:28.361228+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
2020-08-17T09:25:28.361229+00:00 app[web.1]:     rv = self.dispatch_request()
2020-08-17T09:25:28.361229+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
2020-08-17T09:25:28.361230+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2020-08-17T09:25:28.361230+00:00 app[web.1]:   File "/app/app.py", line 14, in index
2020-08-17T09:25:28.361230+00:00 app[web.1]:     result = scrapper_amaz.main_scrapper(amazon_url)
2020-08-17T09:25:28.361231+00:00 app[web.1]:   File "/app/scrapper_amaz.py", line 460, in main_scrapper
2020-08-17T09:25:28.361231+00:00 app[web.1]:     flagged_data = save_to_mongo(data)
2020-08-17T09:25:28.361232+00:00 app[web.1]:   File "/app/scrapper_amaz.py", line 440, in save_to_mongo
2020-08-17T09:25:28.361232+00:00 app[web.1]:     data = collection.find_one({'url':document.get('url')})
2020-08-17T09:25:28.361304+00:00 app[web.1]: AttributeError: 'NoneType' object has no attribute 'get'

这是我的axios请求:

const form = new FormData()
form.append('url', `https://amazon.com/dp/${asin}`)
axios({
  "method": "POST",
  "url": url,
  "data": form,
  "headers": {
    "Content-Type": "multipart/form-data"
  }
}).then((response) => {
  console.log(response)
}).catch((error) => {
  console.log(error)
})

这是我的python app.py:

from flask import Flask, render_template, request, json         # import flask
from flask_cors import CORS
import scrapper_amaz

app = Flask(__name__)             # create an app instance
CORS(app)

@app.route("/", methods = ['GET', 'POST'])                   # at the end point /
def index():                      # call method hello
  if request.method == 'POST':
    amazon_url = request.form['url']
    result = scrapper_amaz.main_scrapper(amazon_url)
    if '_id' in result:
      del result['_id']
    
    result = json.dumps(result)
    result = result.replace('"[','[').replace(']"',']')
    return json.dumps(json.loads(result), indent=2)

  if request.method == 'GET':
    return render_template('index.html')         # which returns "hello world"

if __name__ == "__main__":        # on running python app.py
    app.run(debug=True,host='0.0.0.0', port=5000)                     # run the flask app

有什么问题吗


Tags: inpyappurlflaskherokurequestlib