Python 如何从列表中获取url?美丽之群

Python 如何从列表中获取url?美丽之群,python,beautifulsoup,Python,Beautifulsoup,输出: www = soup1.find_all('a')[1:2] print www [] 如何获取http://www.google.com来自上面的列表?以下是美丽汤的一句话: 您可以通过以下方式读取节点的属性: www[0]['href']这应该可以做到: for link in soup.find_all('a'): print(link.get('href')) # http://example.com/elsie # http://example.com/lacie

输出:

www = soup1.find_all('a')[1:2]
print www
[]

如何获取
http://www.google.com
来自上面的列表?

以下是美丽汤的一句话:


您可以通过以下方式读取节点的属性:
www[0]['href']
这应该可以做到:

for link in soup.find_all('a'):
    print(link.get('href'))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie

因为您知道有一套格式可以打印(例如,前6个字符总是[@salamisalem:糟糕的主意。元素无论如何都不是字符串,而是一个
Tag()
对象。请注意,OP将列表切片而不是索引,因此
www
是一个列表,而不是
Tag
对象。
www[0]['href']
会起作用,或者更好的是,教育他使用索引而不是切片。
for link in soup.find_all('a'):
    print(link.get('href'))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie
www[0].get("href")