>>> 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