Python 烧瓶返回404中的请求
我正在创建我的第一个应用程序,它会得到一堆URL的响应,但当我通过Flask运行它时,只有一个URL得到响应,其余的得到响应[404]。该函数独立工作,只有在通过烧瓶时才会出现问题 我可以看到,在使用print时,URL被传递,但是在打印URL之后,在打印响应时返回404 URL示例:Python 烧瓶返回404中的请求,python,flask,python-requests,Python,Flask,Python Requests,我正在创建我的第一个应用程序,它会得到一堆URL的响应,但当我通过Flask运行它时,只有一个URL得到响应,其余的得到响应[404]。该函数独立工作,只有在通过烧瓶时才会出现问题 我可以看到,在使用print时,URL被传递,但是在打印URL之后,在打印响应时返回404 URL示例: https://www.crimestoppervic.com.au/ https://lunasalon.com.au/ https://bennichomes.com.au/ 功能: def get_pag
https://www.crimestoppervic.com.au/
https://lunasalon.com.au/
https://bennichomes.com.au/
功能:
def get_page_response(url):
response = requests.get(url)
return response
烧瓶:
@app.route('/', methods=['GET', 'POST'])
def index():
form = URLForm()
if form.validate_on_submit():
session['urls'] = form.urls.data
urls = session.get('urls', None)
lines = set(l for l in urls.split("\n") if l)
response_list = []
for url in lines:
response = get_page_response(url)
response_list.append(response)
return render_template('index.html', title='Homepage', form=form, response_list=response_list)
return render_template('index.html', title='Homepage', form=form)
HTML模板:
{% extends "base.html" %}
{% block content %}
<form action="" method="post">
{{ form.hidden_tag() }}
{{ form.urls.label }}<br>
{{ form.urls(rows='20',cols='100') }}</p><br>
{{ form.submit() }}
{% for x in response_list %}
<p>{{ x }}</p>
{% endfor %}
{% endblock %}
如果其他人有相同的问题,答案是删除列表中的\r字符
lines = set(l for l in urls.split("\n") if l)
print(lines) #this will show you that inside the list items have \r
lines = [i.replace("\r", "") for i in lines] #remove \r from each item
你能打印你正在点击的url吗?你能分享
form.url.data
和行的值吗?您确定url.split(“\n”)
会生成有效的URL吗?我已经添加了URL和值的
lines = set(l for l in urls.split("\n") if l)
print(lines) #this will show you that inside the list items have \r
lines = [i.replace("\r", "") for i in lines] #remove \r from each item