问题描述
即使我允许 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() );
?>