Python 在for声明中,我能够得到预期的结果。但是为什么我不能用while语句获得预期的结果呢?
我想用web浏览器检查“”的操作。在for声明中,我能够得到预期的结果。但是在while语句中,我无法得到预期的结果 通过跟踪维基百科的url进行刮取 环境 ・Python 3.6.0 ・瓶子0.13-dev ・mod_wsgi-4.5.15 Apache错误日志 无输出 错误\u空\u响应。 刮削不能完成加工 index.pyPython 在for声明中,我能够得到预期的结果。但是为什么我不能用while语句获得预期的结果呢?,python,python-3.x,beautifulsoup,bottle,Python,Python 3.x,Beautifulsoup,Bottle,我想用web浏览器检查“”的操作。在for声明中,我能够得到预期的结果。但是在while语句中,我无法得到预期的结果 通过跟踪维基百科的url进行刮取 环境 ・Python 3.6.0 ・瓶子0.13-dev ・mod_wsgi-4.5.15 Apache错误日志 无输出 错误\u空\u响应。 刮削不能完成加工 index.py from urllib.request import urlopen from bs4 import BeautifulSoup from bottle impor
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
import datetime
import random
import re
@route('/')
@view("index_template")
def index():
random.seed(datetime.datetime.now())
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
links = getLinks("/wiki/Kevin_Bacon")
while len(links) > 0:
newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
internalLinks.append(newArticle)
links = getLinks(newArticle)
return dict(internalLinks=internalLinks)
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
import datetime
import random
import re
@route('/')
@view("index_template")
def index():
random.seed(datetime.datetime.now())
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
links = getLinks("/wiki/Kevin_Bacon")
for i in range(5):
newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
internalLinks.append(newArticle)
return dict(internalLinks=internalLinks)
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
在for声明中,我能够得到预期的结果
web浏览器输出的结果
['/wiki/Michael_C._Hall', '/wiki/Elizabeth_Perkins',
'/wiki/Paul_Erd%C5%91s', '/wiki/Geoffrey_Rush',
'/wiki/Virtual_International_Authority_File']
index.py
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
import datetime
import random
import re
@route('/')
@view("index_template")
def index():
random.seed(datetime.datetime.now())
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
links = getLinks("/wiki/Kevin_Bacon")
while len(links) > 0:
newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
internalLinks.append(newArticle)
links = getLinks(newArticle)
return dict(internalLinks=internalLinks)
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
from urllib.request import urlopen
from bs4 import BeautifulSoup
from bottle import route, view
import datetime
import random
import re
@route('/')
@view("index_template")
def index():
random.seed(datetime.datetime.now())
html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
internalLinks=[]
links = getLinks("/wiki/Kevin_Bacon")
for i in range(5):
newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
internalLinks.append(newArticle)
return dict(internalLinks=internalLinks)
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
您的
链接
列表的长度永远不会达到0
,因此它将继续运行while循环,直到连接超时
for循环之所以有效,是因为它在一个范围内迭代,所以一旦达到范围最大值,它就会退出
您从未解释过为什么要使用while循环,但如果希望它在一定次数的迭代后退出,则需要使用计数器
counter = 0
# this will exit on the 5th iteration
while counter < 5:
print counter # do something
counter += 1 # increment the counter after each iteration
您是否尝试过添加断点并跟踪代码以查看它的运行情况?或者至少添加一些print
语句来查看它获取的结果?另外,请删除所有与您的问题无关的代码。wsgi代码、视图等。它们使我们很难确定应该关注什么。我删除了wsgi代码。我误解了跟踪链接后,链接列表的长度达到了0,只是为了清楚您没有链接列表,您有一个链接列表;)