【发布时间】:2018-11-24 02:39:22
【问题描述】:
是否有 Python 库可以让我获得任意 HTML sn-p 而不会干扰标记?据我所知,lxml、BeautifulSoup 和 pyquery 都让soup.find(".arbitrary-class") 之类的东西变得容易,但它返回的 HTML 是格式化的。我想要原始的原始标记。
例如,假设我有这个:
<html>
<head>
<title>test</title>
</head>
<body>
<div class="arbitrary-class">
This is some<br />
markup with <br>
<p>some potentially problematic</p>
stuff in it <input type="text" name="w00t">
</div>
</body>
</html>
我想准确地捕捉:
"
This is some<br />
markup with <br>
<p>some potentially problematic</p>
stuff in it <input type="text" name="w00t">
"
...空格和所有,并且不破坏标签以正确格式化(例如<br />)。
问题在于,似乎所有 3 个库似乎都是在内部构造 DOM 并简单地返回一个 Python 对象,该对象表示文件 应该 是什么而不是它 是什么 ,所以我不知道在哪里/如何获得我需要的原始代码 sn-p。
【问题讨论】:
标签: python html web-scraping beautifulsoup lxml