>>> 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()
中返回。我怎樣才能訪問這些屬性?
你爲什麼假設'str()'表示應該匹配實例屬性? 'attrs'在那裏,以及'contents'和'name',所以你在字符串表示中看到的所有內容都可以在實例屬性中找到。 –
@MartijnPieters:'repr'而不是'str',但你的觀點站立! –
@MartijnPieters:我同意在字符串表示中看到的所有內容都可以在實例屬性中找到。但請注意,信息位於屬性'previous_element'和'parent'中。標籤本身的實際內容未顯示。但是,它必須作爲'pprint()'找到它存儲_somewhere_!那麼爲什麼它不會在'__dict __。items()'中返回呢? – dotancohen