微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使在允许 GET 方法之后,flask 也会出现 405 错误

如何解决即使在允许 GET 方法之后,flask 也会出现 405 错误

即使我允许 GET 方法,我的代码也会以错误 405 结束。 当我在本地主机时,它完美地工作,但我在 github 页面或 pythonanywhere 中看到此错误

这里可能有什么问题?

@app.route('/')
def my_home():
    return render_template('index.html')
  
@app.route('/submit',methods=['POST','GET'])
def submit():
      ytlink = request.form['yt link']
      lang = request.form['lang']
      Transcriptions = YouTubeTranscriptApi.get_transcript(ytlink,languages=[lang])
      formatter = textformatter()
      text_formatted = formatter.format_transcript(Transcriptions)
      return text_formatted

这是一些模板:

<!DOCTYPE html>
<html>
    <body>
        <title>Youtube transcription</title>
        <h1>Download your transcription</h1>
        <form action="/submit" method='POST'>
            <label for="ytlink"><strong>YT id of the video:</label></strong>
            <input type="text" id="yt link" name="yt link">
            <br>
            <label for="language"><strong>Language:</label></strong>
            <input type="text" id="lang" name="lang">
            <br>
            <input type="submit" value="Click here to see your transcription",name='submit'>
        </form>

    </body>
</html>

解决方法

只要告诉路由什么时候使用具体的方法即可。

<?php

    $strhtml="<!DOCTYPE html>
        <html lang='en'>
            <head>
                <meta charset='utf-8' />
                <title>Find and Replace</title>
            </head>
            <body>
                <h1>hmmmm</h1>
                <a href='https://www.example.com'>link #1</a>
                <a href='https://www.example.com'>link #2</a>
                <a href='https://www.example.com'>link #3</a>
            </body>
        </html>";
    
    $find='https://www.example.com';
    $repl='https://www.big-yellow-banana.com';
    
    
    $dom=new DOMDocument;
    $dom->loadHTML( $strhtml );
    
    
    $xp=new DOMXpath( $dom );
    $col=$xp->query( sprintf( '//a[ starts-with( @href,"%s" ) ]',$find ) );
    
    if( $col && $col->length > 0 ){
        foreach($col as $node){
            $node->setAttribute('href',str_replace($find,$repl,$node->getAttribute('href') ) );
        }
    }


    printf( 
        'Original:<textarea cols=100 rows=20>%s</textarea>
        <br />
        Modified:<textarea cols=100 rows=20>%s</textarea>
        <br />
        <br />
        Save this document to keep changes.... ie: $dom->saveHTML("/path/to/my-html-file.html");',$strhtml,$dom->saveHTML() );
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。