2013-06-13 81 views
-1

考慮:__dict __項目()沒有返回所有對象的屬性

>>> result = requests.get('http://dotancohen.com') 
>>> soup = BeautifulSoup(result.text) 
>>> a = soup.find('a') 
>>> for k,v in a.__dict__.items(): 
...  print(str(k)+": "+str(v)) 
... 
can_be_empty_element: False 
previous_element: <h1><a class="title" href="/">Dotan Cohen</a></h1> 
next_sibling: None 
name: a 
parent: <h1><a class="title" href="/">Dotan Cohen</a></h1> 
namespace: None 
prefix: None 
previous_sibling: None 
attrs: {'href': '/', 'class': ['title']} 
next_element: Dotan Cohen 
parser_class: <class 'bs4.BeautifulSoup'> 
hidden: False 
contents: ['Dotan Cohen'] 
>>> pprint(a) 
<a class="title" href="/">Dotan Cohen</a> 
>>> 

的值pprint的回報是不是任何屬性,這些屬性__dict__.items()回報的價值。這對我來說意味着的屬性不會在__dict__.items()中返回。我怎樣才能訪問這些屬性?

+2

你爲什麼假設'str()'表示應該匹配實例屬性? 'attrs'在那裏,以及'contents'和'name',所以你在字符串表示中看到的所有內容都可以在實例屬性中找到。 –

+0

@MartijnPieters:'repr'而不是'str',但你的觀點站立! –

+0

@MartijnPieters:我同意在字符串表示中看到的所有內容都可以在實例屬性中找到。但請注意,信息位於屬性'previous_element'和'parent'中。標籤本身的實際內容未顯示。但是,它必須作爲'pprint()'找到它存儲_somewhere_!那麼爲什麼它不會在'__dict __。items()'中返回呢? – dotancohen

回答

2

實例字典中沒有缺失屬性。讓我們來看看元素的表示:

<a class="title" href="/">Dotan Cohen</a> 

我們有一個標籤名(a),屬性(titlehref,其值),我們有文本內容(Dotan Cohen)。這些都是所有出現在實例屬性您列出:

  • name: a
  • attrs: {'href': '/', 'class': ['title']}
  • contents: ['Dotan Cohen']

contents是這個元素的直接後裔的名單;只有一個,文本對象(NavigableString實例使用看起來就像一個常規字符串)。可以使用vars() built-in API function來列出實例屬性。我看到你已經在使用pprint();而不是循環使用.items(),只需使用pprint(vars(a))並保存您輸入的完整循環;作爲獎金pprint()排序項第一:

>>> pprint(vars(a)) 
{'attrs': {'class': ['title'], 'href': '/'}, 
'can_be_empty_element': False, 
'contents': [u'Dotan Cohen'], 
'hidden': False, 
'name': 'a', 
'namespace': None, 
'next_element': u'Dotan Cohen', 
'next_sibling': None, 
'parent': <h1><a class="title" href="/">Dotan Cohen</a></h1>, 
'parser_class': <class 'bs4.BeautifulSoup'>, 
'prefix': None, 
'previous_element': <h1><a class="title" href="/">Dotan Cohen</a></h1>, 
'previous_sibling': None} 

你正在尋找由.__repr__()掛鉤的元素類的內置的字符串:

>>> a.__repr__() 
'<a class="title" href="/">Dotan Cohen</a>' 

repr()上的使用通常被稱爲object:

>>> repr(a) 
'<a class="title" href="/">Dotan Cohen</a>' 

該字符串是根據您在對象屬性中看到的已分析元素信息構建的。

+0

從我的理解你的解釋,標籤的實際返回值沒有存儲在任何地方?這意味着'pprint()'返回的值必須由某種'ToString()'方法來完成。我可以通過查看解析的網站的源代碼來確認,HTML中的屬性順序與'pprint()'返回的字符串中的屬性順序不同。 – dotancohen

+1

是的,您正在查看對象的'repr()'結果。 '__repr__'方法負責從屬性數據構建。 HTML屬性沒有排序(如Python字典)。 –

+0

我明白了,謝謝Martijn。從谷歌搜索了一下,我發現存在一個'dir()'方法,它將返回'a'的所有'名稱',其中之一是'__repr__'。 'a .__ repr__'確實返回'<綁定方法標籤.__ unicode__的Dotan Cohen>''。但是,除了「名稱是:變量,模塊,函數等」之外,我很難找到「名稱」這個詞的定義。 – dotancohen

相關問題