要使用Python在HTML中提取innerhtml,可以使用BeautifulSoup库
pip install beautifulsoup4
然后,您可以使用以下代码提取HTML元素的innerhtml:
from bs4 import BeautifulSoup
html_content = '''
<html>
<head>
<title>示例</title>
</head>
<body>
<div id="example">
这里是示例文本。
</div>
</body>
</html>
'''
# 创建BeautifulSoup对象
soup = BeautifulSoup(html_content, 'html.parser')
# 获取指定ID的元素
example_div = soup.find('div', {'id': 'example'})
# 提取元素的innerhtml
inner_html = example_div.decode_contents()
print(inner_html)
这将输出:
这里是示例文本。
在这个例子中,我们首先创建了一个BeautifulSoup对象,然后使用find
方法获取具有指定ID的元素。最后,我们使用decode_contents()
方法提取元素的innerhtml。
领取专属 10元无门槛券
手把手带您无忧上云