相关疑难解决方法(0)

为什么dict.get(key)而不是dict [key]?

今天,我遇到了这个dict方法get,它给出了字典中的一个键,返回了相关的值.

这个功能用于什么目的?如果我想找到与字典中的键相关联的值,我可以这样做dict[key],它返回相同的东西:

dictionary = {"Name": "Harry", "Age": 17}
dictionary["Name"]
dictionary.get("Name")
Run Code Online (Sandbox Code Playgroud)

python dictionary

553
推荐指数
11
解决办法
46万
查看次数

python中[:]的含义是什么?

该行在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

python beautifulsoup web-scraping python-2.7

11
推荐指数
1
解决办法
6万
查看次数