Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助找出python代码中的缩进错误吗_Python - Fatal编程技术网

需要帮助找出python代码中的缩进错误吗

需要帮助找出python代码中的缩进错误吗,python,Python,我在尝试运行下面的代码时遇到缩进错误。我试图递归地打印出一组html页面的URL import urllib2 from BeautifulSoup import * from urlparse import urljoin # Create a list of words to ignore ignorewords=set(['the','of','to','and','a','in','is','it']) def crawl(self,pages,depth=2): for i in r

我在尝试运行下面的代码时遇到缩进错误。我试图递归地打印出一组html页面的URL

import urllib2
from BeautifulSoup import *
from urlparse import urljoin
# Create a list of words to ignore
ignorewords=set(['the','of','to','and','a','in','is','it'])

def crawl(self,pages,depth=2):
for i in range(depth):
newpages=set()
for page in pages:
try:
c=urllib2.urlopen(page)
except:
print "Could not open %s" % page
continue
soup=BeautifulSoup(c.read())
self.addtoindex(page,soup)
links=soup('a')
for link in links:
if ('href' in dict(link.attrs)):
url=urljoin(page,link['href'])
if url.find("'")!=-1: continue
url=url.split('#')[0] # remove location portion
if url[0:4]=='http' and not self.isindexed(url):
newpages.add(url)
linkText=self.gettextonly(link)
self.addlinkref(page,url,linkText)
self.dbcommit()
pages=newpages

好吧,你的代码是完全没有牵连的,所以当你尝试运行它时Python会哭

请记住,在Python中,空格很重要。使用4个空格而不是制表符缩进可以节省大量“不可见”的缩进错误


我投了反对票,因为代码粘贴未格式化/未缩进,这意味着海报不理解python(也没有阅读基本教程),或者粘贴代码时没有重新缩进,这使得任何人都无法回答。

您在哪里复制了此代码?您的代码没有正确缩进(对我来说,这就像是一个复制/粘贴),你应该看一看,了解python中正确的缩进。试着缩进你的代码,兄弟。这样我们就不用头疼了。