BS4错误消息“没有属性'renderContents'”

2024-06-16 15:32:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我从运行下面的代码得到一条错误消息。在

代码:

from bs4 import BeautifulSoup
import urllib2
import re

url="http://m.harveynorman.com.au/ipods-audio-music/ipods/ipods"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
A = soup.findAll('strong',{'class':'name fn'})[0]
for B in A:
   print B.renderContents()

错误消息:

^{pr2}$

Tags: 代码fromimportrehttp消息url错误
1条回答
网友
1楼 · 发布于 2024-06-16 15:32:06

在本例中,A是给定类型的单个结果。在

>>> type(A)
<class 'bs4.element.Tag'>

当您遍历此对象A时,将生成一个对象B:

^{pr2}$

在本例中,bs4.element.NavigableString实际上并不包含方法renderContents。这可以通过以下方式进行验证:

>>> dir(B)
['HTML_FORMATTERS', 'PREFIX', 'SUFFIX', 'XML_FORMATTERS', '__add__', '__class__', '__contains__', '__copy__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attr_value_as_string', '_attribute_checker', '_find_all', '_find_one', '_formatter_field_name_split', '_formatter_for_name', '_formatter_parser', '_is_xml', '_lastRecursiveChild', '_last_descendant', '_tag_name_matches_and', 'append', 'attribselect_re', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'extract', 'fetchNextSiblings', 'fetchParents', 'fetchPrevious', 'fetchPreviousSiblings', 'find', 'findAllNext', 'findAllPrevious', 'findNext', 'findNextSibling', 'findNextSiblings', 'findParent', 'findParents', 'findPrevious', 'findPreviousSibling', 'findPreviousSiblings', 'find_all_next', 'find_all_previous', 'find_next', 'find_next_sibling', 'find_next_siblings', 'find_parent', 'find_parents', 'find_previous', 'find_previous_sibling', 'find_previous_siblings', 'format', 'format_string', 'index', 'insert', 'insert_after', 'insert_before', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'name', 'next', 'nextGenerator', 'nextSibling', 'nextSiblingGenerator', 'next_element', 'next_elements', 'next_sibling', 'next_siblings', 'output_ready', 'parent', 'parentGenerator', 'parents', 'partition', 'previous', 'previousGenerator', 'previousSibling', 'previousSiblingGenerator', 'previous_element', 'previous_elements', 'previous_sibling', 'previous_siblings', 'replace', 'replaceWith', 'replaceWithChildren', 'replace_with', 'replace_with_children', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'setup', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'tag_name_re', 'title', 'translate', 'unwrap', 'upper', 'wrap', 'zfill']

以下是您可能真正想要的(没有findAll上的切片标记):

A = soup.findAll('strong',{'class':'name fn'})  # Notice there is no [0] slice notation here.
for B in A:
    print B.renderContents()

相关问题 更多 >