Sou*_*rav 11 python beautifulsoup web-scraping python-2.7
该行在del taglist[:]
下面的代码中做了什么?
import urllib
from bs4 import BeautifulSoup
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
Run Code Online (Sandbox Code Playgroud)
问题是"编写一个扩展到http://www.pythonlearn.com/code/urllinks.py的Python程序.该程序将使用urllib从下面的数据文件中读取HTML,从锚点中提取href = vaues标签,扫描相对于列表中第一个名称的特定位置的标签,按照该链接重复该过程多次并报告您找到的姓氏".示例问题:从http://python-data.dr-chuck.net/known_by_Fikret.html开始 查找位置3的链接(名字是1).请关注该链接.重复此过程4次.答案是您检索的姓氏.姓名序列:Fikret Montgomery Mhairade Butchi Anayah姓氏顺序:Anayah
kha*_*hyk 17
[:]
是数组中每个元素的数组切片语法.
这里的答案更深入一般用途:解释Python的切片表示法
del arr # Deletes the array itself
del arr[:] # Deletes all the elements in the array
del arr[2] # Deletes the second element in the array
del arr[1:] # etc..
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
58891 次 |
最近记录: |