检索<a> tag</a>的属性

时间:2013-06-24 01:43:58

标签: python parsing beautifulsoup

我有这个:

<a class = "__cf_email__" 
  data-cfemail = "c2a3aca6b0a7a3eca1ada382a5afa3abaeeca1adaf"
  href = "http://www.cloudflare.com/email-protection">[email protected]</a>

我需要获取此字符串c2a3aca6b0a7a3eca1ada382a5afa3abaeeca1adaf。有任何想法吗?不知道我是怎么尝试上课但是可以得到字符串

autor = soup.findAll("a", {"class": "__cf_email__"}, limit = 1)

2 个答案:

答案 0 :(得分:4)

Tag的属性可通过__getitem__方法访问。换句话说,"you can access a tag's attributes by treating the Tag object as though it were a dictionary."

>>> autor = soup.find("a", {"class": "__cf_email__"})
>>> print autor["data-cfemail"]
'c2a3aca6b0a7a3eca1ada382a5afa3abaeeca1adaf'

答案 1 :(得分:0)

您是否有意获得data-cfemail的{​​{1}}属性?