我从这里的教程(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
为什么我需要在此循环的链接中定义链接?逻辑是什么?谢谢你的帮助。