2017-10-18 30 views
0

試圖使用python使用python

from urllib2 import urlopen 
from bs4 import BeautifulSoup 
import re 

pages = set() 
def getLinks (pageUrl): 
    global pages 
html = urlopen("http://www.mysite.ch" + pageUrl) 
bsObj = BeautifulSoup(html,"html.parser") 
for link in bsObj.findAll("a",href = re.compile("^(/f/)")): 
    if 'href' in link.attrs: 
     if link.attrs['href'] not in pages: 
      newPage = link.attrs['href'] 
      print(newPage) 
      pages.add(newPage) 
       getLinks(newPage) 

getLinks( 「」)

我有以下錯誤味精建立一個網站地圖創建一個站點地圖:

File "sitemap builder.py", line 8, in <module> 
    html = urlopen("http://www.mysite.ch" + pageUrl) 
    NameError: name 'pageUrl' is not defined 

如果somepeople有想法你是歡迎的(對不好的縮進,我同意它不好)

+0

這是完整的錯誤跟蹤? – MrPyCharm

+0

你確定這是完整的錯誤信息嗎?這似乎並不像它的完整的錯誤消息 –

+0

@GerriePretorius現在完全錯誤跟蹤,對不起 –

回答

0

好吧,讓我們假設你的代碼片段張貼是正確的,確實格式正確。然後,看着8號線,在那裏你有錯誤,清楚地表明,

pageUrl 

是在全球範圍內,而它在

def getLinks (pageUrl): 

範圍被宣佈無論你需要聲明PAGEURL或者您需要正確格式化您的代碼。