-1

考虑:

>>> 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()值。这对我来说意味着存在的属性a没有在__dict__.items(). 我如何访问这些属性?

4

1 回答 1

2

实例字典中没有缺少属性。我们来看看元素的表示:

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

我们有一个标签名称 ( a)、属性 (titlehref, 带有值),我们有文本内容 ( Dotan Cohen)。这些都存在于您列出的实例属性中:

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

contents是该元素的直接后代列表;只有一个,一个文本对象(NavigableString实例使用看起来就像一个常规字符串的表示)。

您可以使用vars()内置 API 函数列出实例属性。我看到你已经在使用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()

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

该字符串是根据您在对象属性中看到的已解析元素信息构建的。

于 2013-06-13T09:25:35.050 回答