Python urllib.urlopen只运行一次
我正在上Coursera的一门在线课程,其中一项作业让我耽误了几分钟。我不是要求任何人解决它,只是。。。一些指导可能会有所帮助:)Python urllib.urlopen只运行一次,python,python-2.7,Python,Python 2.7,我正在上Coursera的一门在线课程,其中一项作业让我耽误了几分钟。我不是要求任何人解决它,只是。。。一些指导可能会有所帮助:) 我的问题是urllib没有运行多次,只有一次。我知道我做错了什么,但就是想不出来:/我不确定你说的它只运行一次是什么意思,但我猜你在这里遇到了一个问题: for tag in tags: taglist.append(tag) 在循环的每一次迭代中,您都会将标记列表添加到taglist,保留其中的所有内容,因此在第一次迭代之后,taglist[pos]始终
我的问题是urllib没有运行多次,只有一次。我知道我做错了什么,但就是想不出来:/我不确定你说的它只运行一次是什么意思,但我猜你在这里遇到了一个问题:
for tag in tags:
taglist.append(tag)
在循环的每一次迭代中,您都会将标记列表添加到taglist
,保留其中的所有内容,因此在第一次迭代之后,taglist[pos]
始终只指向相同的url
您需要在重新填充标记列表之前清除它,例如
taglist = list()
for tag in tags:
taglist.append(tag)
我不确定你说它只运行一次是什么意思,但我猜你在这里遇到了一个问题:
for tag in tags:
taglist.append(tag)
在循环的每一次迭代中,您都会将标记列表添加到taglist
,保留其中的所有内容,因此在第一次迭代之后,taglist[pos]
始终只指向相同的url
您需要在重新填充标记列表之前清除它,例如
taglist = list()
for tag in tags:
taglist.append(tag)
马可清理你的标签是对的。但是,尽管您的代码现在可以正常运行,但仍然无法获得预期的结果。 更改为: url=taglist[pos]。获取('href',无) 到 url=taglist[pos-1]。获取('href',无)
希望能有所帮助。马可正确地清除了你的标记列表。但是,尽管您的代码现在可以正常运行,但仍然无法获得预期的结果。 更改为: url=taglist[pos]。获取('href',无) 到 url=taglist[pos-1]。获取('href',无)
希望有帮助。似乎是一个好方法,我也在学习同样的在线课程。您应该在每次迭代中初始化标记列表,以使列表中的每个“pos”项都是所需的新url,而不是追加标记列表。 下面是对for循环的修改:
for i in range(count):
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags = soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[pos].get('href', None)
print 'Retrieving: ', url
urllist.append(url)
taglist = list()
print 'Last Url: ', urllist[-1]
这似乎是一个很好的方法,我也参加了同样的在线课程。您应该在每次迭代中初始化标记列表,以使列表中的每个“pos”项都是所需的新url,而不是追加标记列表。 下面是对for循环的修改:
for i in range(count):
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags = soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[pos].get('href', None)
print 'Retrieving: ', url
urllist.append(url)
taglist = list()
print 'Last Url: ', urllist[-1]
简化如下:
在Python2.7和BeautifulSoup4上对我进行了完美的测试和工作
import urllib
from BeautifulSoup import *
taglist=list()
url=raw_input("Enter URL: ")
count=int(raw_input("Enter count:"))
position=int(raw_input("Enter position:"))
for i in range(count):
print "Retrieving:",url
html=urllib.urlopen(url).read()
soup=BeautifulSoup(html)
tags=soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[position-1].get('href', None)
del taglist[:]
print "Retrieving:",url
每次成功迭代后,在代码中清除标记列表:
for i in range(count):
html = urllib.urlopen(urllist[-1]).read()
soup = BeautifulSoup(html)
tags = soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[pos].get('href', None)
print 'Retrieving: ', url
urllist.append(url)
del taglist[:] \add this line to clear the taglist
简化如下:
在Python2.7和BeautifulSoup4上对我进行了完美的测试和工作
import urllib
from BeautifulSoup import *
taglist=list()
url=raw_input("Enter URL: ")
count=int(raw_input("Enter count:"))
position=int(raw_input("Enter position:"))
for i in range(count):
print "Retrieving:",url
html=urllib.urlopen(url).read()
soup=BeautifulSoup(html)
tags=soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[position-1].get('href', None)
del taglist[:]
print "Retrieving:",url
每次成功迭代后,在代码中清除标记列表:
for i in range(count):
html = urllib.urlopen(urllist[-1]).read()
soup = BeautifulSoup(html)
tags = soup('a')
for tag in tags:
taglist.append(tag)
url = taglist[pos].get('href', None)
print 'Retrieving: ', url
urllist.append(url)
del taglist[:] \add this line to clear the taglist
给我们看实际输出,给我们看实际输出,就是这样,很简单,我忽略了它。我将在未来的项目中牢记这一点。非常感谢。就是这样,很简单,我忽略了它。我将在未来的项目中牢记这一点。非常感谢你。