目的是打開頁面獲取網址18號,交換URL位置18和重新運行7次,但我的代碼卡住後獲得位置18,爲什麼ELIF不在24行? (無回溯,程序只是坐在那裏)elif語句沒有運行?
import urllib
from BeautifulSoup import *
#not the real url
url= ('http://abc.googl.com')
count=7
position = 0
#n will be used to check that the code i running
n=0
while count >= 0:
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags = soup('a')
for tag in tags:
x= tag.get('href', None)
position=position+1
if position <= 18:
n=n+1
print 'calculating', n
print x
elif position == 18:
url=(x)
print 'new url', x
count=count-1
print 'new count', count
position=0
if count == 0:
print "done"
print x
改變了if的值,現在唯一的問題是,它達不到計數0時不會停止。我會想出來:)謝謝大家的答案,非常有幫助! – Schwarz
請不要編輯您的問題的解決方案。相反,您可以將*答案中的一個*標記爲「已接受」,以表明您找到了最有用的答案(並且不標記答案也很好)。請參閱[如何接受答案的工作?](https://meta.stackexchange.com/q/5234)瞭解更多信息。 –