获取错误“异常消息为:400错误请求:浏览器(或代理)发送了此服务器无法理解的请求”

2024-04-19 11:27:55 发布

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

我收到“异常消息为:400错误请求:浏览器(或代理)发送了此服务器无法理解的请求。”执行此代码时出错。我没有得到提示异常块的错误原因。请帮忙

下面是我的Python flask代码

import os
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS, cross_origin
import requests
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen as uReq

app = Flask(__name__)


@app.route('/', methods=['GET'])  
@cross_origin() 
def homePage():
   return render_template("index.html")

@app.route('/review', methods=['POST', 'GET'])  # route to show the review comments in a web UI
@cross_origin()
def index():
   if request.method == 'POST':
       try:
         searchString = request.form['content'].replace(" ", "") 
         flipkart_url = "https://www.flipkart.com/search?q=" + searchString
         uClient = uReq(flipkart_url) 
         flipkartPage = uClient.read() 
         uClient.close()
         flipkart_html = bs(flipkartPage, "html.parser") 
         bigboxes = flipkart_html.findAll("div", {"class": "bhgxx2 col-12-12"}) 
         del bigboxes[0:3]
         box = bigboxes[0]
         productLink = "https://www.flipkart.com" + box.div.div.div.a['href'] 
         prodRes = requests.get(productLink)
         prodRes.encoding = 'utf-8' 
         prod_html = bs(prodRes.text, "html.parser")
         commentboxes = prod_html.find_all('div', {'class': "_3nrCtb"}) 
   
         filename = searchString + ".csv"
         fw = open(filename, "w")
         headers = "Product, Customer Name, Rating, Heading, Comment \n"
         fw.write(headers)
        
         reviews = []
         for commentbox in commentboxes:
             try:
                 # name.encode(encoding='utf-8')
                 name = commentbox.div.div.find_all('p', {'class': '_3LYOAd _3sxSiS'})[0].text
             except:
                 name = 'No Name'
             try:
                 # rating.encode(encoding='utf-8')
                 rating = commentbox.div.div.div.div.text
             except:
                 rating = 'No Rating'
             try:
                 # commentHead.encode(encoding='utf-8')
                 commentHead = commentbox.div.div.div.p.text
             except:
                 commentHead = 'No Comment Heading'
             try:
                 comtag = commentbox.div.div.find_all('div', {'class': ''})
                 # custComment.encode(encoding='utf-8')
                 custComment = comtag[0].div.text
             except Exception as e:
                 print("Exception while creating dictionary: ", e)

             mydict = {"Product": searchString, "Name": name, "Rating": rating, "CommentHead": commentHead,"Comment": custComment}
             reviews.append(mydict)
         return render_template('results.html', reviews=reviews[0:(len(reviews) - 1)])

      except Exception as e:
         print('The Exception message is: ', e)
         return 'something is wrong'

   else:
     return render_template('index.html')
if __name__ == "__main__":
   #app.run(host='0.0.0.0', port=5000)
   #app.run(host='0.0.0.0', port=port) #for sever
   app.run(host='127.0.0.1', port=8001, debug=True)

下面是我的HTML代码文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<title>Welcome to Review Scrapper</title>
<link rel="stylesheet" href="../static/css/fonts/all.css">
<link rel="stylesheet" type="text/css" href="../static/css/main.css">

<nav id='navbar'>
 <text class="logo">Review Scrapper</text>
 <a href='#content'>Home</a>
 <a href='https://github.com/rs301378'>Github</a>
 <a href='https://www.linkedin.com/in/rohit-sharma-52b369158/'>Portfolio</a>
</nav>

<body>

<div class="content">

    <form action="/review" method="POST">
        <input class="search-box-input" type="text" id="content" placeholder="Search anything...">
        <button class="search-box-button" type="submit" value="Search">Search</button>
    </form>

  </div>

 <footer>
  Made with <i class='fas fa-heart'></i> by Rohit Sharma
 </footer>
 </body>
 </html>

Tags: textnameimportdivapphtmlcontentutf
1条回答
网友
1楼 · 发布于 2024-04-19 11:27:55

你的烧瓶部分没问题。问题是您的html表单。html表单中的每个输入都通过属性名进行访问。如果调用form.request["content"],则希望获得名为content的输入值。您的输入甚至没有属性名(只是一个带有名称内容的id,但不是engough)

更改<input>元素(添加name="content"):

<input name="content" class="search-box-input" type="text" id="content" placeholder="Search anything...">

相关问题 更多 >