环境
·Python 3.6.0
·瓶装0.13-dev
·mod_wsgi的-4.5.15
在网络上尝试以下代码会导致500错误
500内部服务器错误
应用程序/ WSGI
# -*- coding:utf-8 -*-
import sys, os
dirpath = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dirpath)
sys.path.append('../')
os.chdir(dirpath)
import bottle
import index
application = bottle.default_app()
index.py
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in links.attr:
internalLinks.append(links.attr['href'])
return dict(internalLinks=internalLinks)
视图/ index_template.tpl
{{internalLinks}}
apache日志
[error] mod_wsgi (pid=23613): Target WSGI script '/app.wsgi' cannot be loaded as Python module.
[error] mod_wsgi (pid=23613): Exception occurred processing WSGI script '/app.wsgi'.
[error] Traceback (most recent call last):
[error] File "/app.wsgi", line 8, in <module>
[error] import index
[error] File "/index.py", line 11
[error] for link in bsObj.findAll("a"):
[error] ^
[error] IndentationError: unexpected indent
答案 0 :(得分:2)
日志报告IndentationError
,因此代码中的缩进有问题:具体来说,for
循环过度缩进,for
语句应该是与bsObj
作业处于同一级别。
您还需要使变量名称保持一致(link
| links
)并使用attrs
属性,而不是attr
。固定代码如下(未经测试)。
@route('/')
@view("index_template")
def index():
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
bsObj = BeautifulSoup(html, "html.parser")
for link in bsObj.findAll("a"):
if 'href' in link.attrs:
internalLinks.append(link.attrs['href'])
return dict(internalLinks=internalLinks)