无法从标记中检索HREF

时间:2015-11-16 06:42:00

标签: python beautifulsoup

您好我正在尝试从HTML页面检索第三个HREF标记。但是,以下代码不会返回任何结果。任何提示/答案都非常有用。我想解析的URL如下:

http://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Fikret.html

代码是这样的:

 # This python code will parse an html and 
   # find href content from a particular position

   import urllib
   from BeautifulSoup import *

   url = raw_input('Enter - ')
   html = urllib.urlopen(url).read()
   soup = BeautifulSoup(html)

   # Retrieve all of the anchor tags
   tags = soup('a',limit=3)[2]
   for tag in tags:
      print tag.get('href', None)

1 个答案:

答案 0 :(得分:0)

使用

tags = soup('a', limit=3)[2]

print tags.get('href', None)

或删除[2]

tags = soup('a', limit=3)

for tag in tags:
    print tag.get('href', None)