PHP HTML DOM:如何选择所有可见/可读文本?
我试图抓取网站,通过保留 html 结构来修改所有可见文本(意思是:链接、段落、标题等),然后渲染“新”页面。
基本上我想在不破坏设计/功能的情况下打乱所有可读文本。
我用 Zend_Dom_Query 尝试过,但如何仅选择文本?
$dom = new Zend_Dom_Query($html);
$results = $dom->query( ??? );
或者还有另一种/更好的方法吗?
预先非常感谢。
示例
输入:
<html>
<head>....</head>
<body>
<div>
<h1>Headline</h1>
<h2>Subheadline</h2>
<p>Some text</p>
<a href="...">
A Link
<img src="..." />
<span style="display:none">additional text</span>
</a>
</div>
</body>
</html>
输出:
<html>
<head>....</head>
<body>
<div>
<h1>Hinladee</h1>
<h2>Suialebdhne</h2>
<p>Smoe txet</p>
<a href="...">
A Lnik
<img src="..." />
<span style="display:none">anodiaditl txet</span>
</a>
</div>
</body>
</html>
I'm trying to scrape websites, modify all visible text (meaning: links, paragraphs, headlines, etc) by keeping the html structure and then render the 'new' page afterwards.
Basically I want to scramble all readable text without destroying the design/functionality.
I tried it with Zend_Dom_Query, but how to select just text?
$dom = new Zend_Dom_Query($html);
$results = $dom->query( ??? );
Or is there another/better way of doing this?
Thanks a lot in advance.
Example
Input:
<html>
<head>....</head>
<body>
<div>
<h1>Headline</h1>
<h2>Subheadline</h2>
<p>Some text</p>
<a href="...">
A Link
<img src="..." />
<span style="display:none">additional text</span>
</a>
</div>
</body>
</html>
Output:
<html>
<head>....</head>
<body>
<div>
<h1>Hinladee</h1>
<h2>Suialebdhne</h2>
<p>Smoe txet</p>
<a href="...">
A Lnik
<img src="..." />
<span style="display:none">anodiaditl txet</span>
</a>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试此服务:http://www.alchemyapi.com/api/text/ - 它的 API 提供了易于使用的机制,可以从任何网页中提取页面文本和标题信息。这是一个简单的方法。其他方法是使用 http://www.alchemyapi.com/api/scrape/
You can try this service: http://www.alchemyapi.com/api/text/ - its API provides easy-to-use mechanisms to extract page text and title information from any web page. It's a simple way. Other way is to use http://www.alchemyapi.com/api/scrape/
解决方案:
感谢@Yoshi 和@Gordon。这正是我一直在寻找的:
Solution:
Thanks to @Yoshi and @Gordon. This is exactly what I was looking for: