Docker部署FlareSolverr后,Flask+Python调用报405 Method Not Allowed错误
问题解决:FlareSolverr调用出现405 Method Not Allowed错误
问题原因
你用cURL发起的是POST请求,但Python代码里误用了requests.get()(GET请求),而FlareSolverr的/v1接口仅支持POST方法,因此返回405错误。
修复代码
把requests.get()替换为requests.post(),同时用json参数自动处理数据序列化:
import requests import time from bs4 import BeautifulSoup def get_parsed_page(url, delay=0.5): data = { "cmd": "request.get", "url": url, "maxTimeout": 60000 } headers = {"Content-Type": "application/json"} time.sleep(delay) # 改用POST请求,用json参数自动序列化字典为JSON格式 response = requests.post("***:8191/v1", headers=headers, json=data) print(response) return BeautifulSoup(response.text, 'lxml')
额外说明
- 使用
json=data替代data=data,requests会自动完成字典到JSON字符串的转换,同时自动适配Content-Type头 - 原代码重复发起了两次请求,修复后仅调用一次并保存响应到变量,提升执行效率
内容的提问来源于stack exchange,提问作者user20766085
相关产品推荐
相关产品推荐

