python技术是什么意思_python [:]的含义是什么

What does the line del taglist[:] do in the code below?

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

The question is "write a Python program that expands on http://www.pythonlearn.com/code/urllinks.py. The program will use urllib to read the HTML from the data files below, extract the href= vaues from the anchor tags, scan for a tag that is in a particular position relative to the first name in the list, follow that link and repeat the process a number of times and report the last name you find".

Sample problem: Start at http://python-data.dr-chuck.net/known_by_Fikret.html

Find the link at position 3 (the first name is 1). Follow that link. Repeat this process 4 times. The answer is the last name that you retrieve.

Sequence of names: Fikret Montgomery Mhairade Butchi Anayah

Last name in sequence: Anayah

解决方案

[:] is the array slice syntax for every element in the array.

This answer here goes more in depth of the general uses: Explain Python's slice notation

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..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值