-1

我从这里的教程(http://jeriwieringa.com/blog/2012/11/04/beautiful-soup-tutorial-part-1/)运行此代码:

from bs4 import BeautifulSoup

soup = BeautifulSoup (open("43rd-congress.htm"))

final_link = soup.p.a
final_link.decompose()

links = soup.find_all('a')
for link in links:
names = link.contents[0]
fullLink = link.get('href')
print names
print fullLink

我得到这个错误:

File "soupexample.py", line 11, in <module>
fullLink = link.get('href')
link is not defined

为什么我需要在此循环的链接中定义链接?逻辑是什么?谢谢你的帮助。

4

1 回答 1

2

我想错误来自这里(不知何故,示例中没有缩进,当然应该有):

for link in links:
    names = link.contents[0]
    fullLink = link.get('href')
    print names
    print fullLink
于 2013-10-21T01:18:11.137 回答