use*_*028 6 html python beautifulsoup html-parsing python-2.7
我使用的是python2.7.6,urllib2和BeautifulSoup
从网站中提取html并存储在变量中.
如何div使用beautifulsoup 显示带有id 的html内容?
<div id='theDiv'>
<p>div content</p>
<p>div stuff</p>
<p>div thing</p>
Run Code Online (Sandbox Code Playgroud)
将会
<p>div content</p>
<p>div stuff</p>
<p>div thing</p>
Run Code Online (Sandbox Code Playgroud)
ale*_*cxe 11
加入div标签的元素.contents:
from bs4 import BeautifulSoup
data = """
<div id='theDiv'>
<p>div content</p>
<p>div stuff</p>
<p>div thing</p>
</div>
"""
soup = BeautifulSoup(data)
div = soup.find('div', id='theDiv')
print ''.join(map(str, div.contents))
Run Code Online (Sandbox Code Playgroud)
打印:
<p>div content</p>
<p>div stuff</p>
<p>div thing</p>
Run Code Online (Sandbox Code Playgroud)