Python Beautifulsoup:当我尝试访问soup.head.next_与Beautifulsoup4的兄弟姐妹值时,获得了一个新行
我正在尝试一个来自BeautifulSoup的例子,发现它的行为很奇怪。当我尝试访问下一个\u同级值时,图片中会出现“\n”而不是“body”Python Beautifulsoup:当我尝试访问soup.head.next_与Beautifulsoup4的兄弟姐妹值时,获得了一个新行,python,python-2.7,web,web-scraping,beautifulsoup,Python,Python 2.7,Web,Web Scraping,Beautifulsoup,我正在尝试一个来自BeautifulSoup的例子,发现它的行为很奇怪。当我尝试访问下一个\u同级值时,图片中会出现“\n”而不是“body” html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b><
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
soup.head.next_sibling
u'\n'
html_doc=”“”
睡鼠的故事
睡鼠的故事
从前有三个小姐妹,她们的名字是
,
和
;
他们住在井底
"""
从bs4导入BeautifulSoup
汤=美汤(html\U文档)
汤,头,下一个兄弟姐妹
u'\n'
我正在使用最新版本的beautifulSoup4。i、 e 4.3.2。请帮帮我。
提前谢谢。试试这个
soup.head.find_next_sibling()
或
HTML中有3个BeautifulSoup
“sees”:
标签
NavigableString
注释
.next\u sibling
时,它将返回当前对象之后的下一个对象,在您的情况下,该对象是文本节点(navigablesting
)。在文档中解释
如果要查找当前标记之后的下一个标记,请使用或,并指定标记名:find\u next\u sibling(“body”)
您还可以使用“下一个兄弟姐妹”:
感谢alecxe的详细解释。
soup.head.next_sibling.next_sibling
soup.select("head + *")