IndentationError: unexpected indent

エラー

問題発生したコード

 状態:-  閲覧数:576  投稿日:2017-10-15  更新日:2017-10-15  
▼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)



エラーメッセージ


Apacheログ
▼/var/log/httpd/
[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


エラー対応


「不要なインデント」削除
・forループがインデントされている
・forステートメントはbsObjの割り当てと同じレベルにする必要がある

変数名が一貫していない
・link|links
・attrではなくattrs属性を使用する必要がある
・links.attr → link.attrs


修正後コード


▼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 link.attrs:
           internalLinks.append(link.attrs['href'])
   return dict(internalLinks=internalLinks)



I want to pass the array to the template of bottle and display its contents


NameError: name 're' is not defined

SyntaxError: invalid syntax

コメント投稿(ログインが必要)