Coldfusion - 简单的HTML解析

use*_*264 3 regex coldfusion

我们目前有一些文章发布到我们的网站上.它们可以与以下类型的html一起出现

<p>this is an article<br>
<img src="someimage">
</p>

<p>this is an article<br>
<img src="someimage">
</p>

<p>this is an article<br>
<img src="someimage">
</p>

<p>this is an article<br>
<img src="someimage">
</p>
Run Code Online (Sandbox Code Playgroud)

要么

<p><img src="someimage">
this is an article<br>
</p>
<p>this is an article<br>
<img src="someimage">
</p>
<p><img src="someimage">
this is an article<br>
</p>
Run Code Online (Sandbox Code Playgroud)

有时其他一些html标签可能在里面,我无法理解如何使用coldfusion来刮取页面来实现这一点

基本上我需要做的是抓住第一段文字和图像,并能够安排它.

这可能使用Coldfusion 8吗?有人能够指出我如何学习这个方向吗?

Mic*_*umo 6

100%绝对可能!

现在,不要被我要提出的建议所拖延,实际上很容易理解这一点.

下载一个名为jSoup的库...它的唯一目的是从网页中的DOM中抓取内容:

http://jsoup.org/

然后,您可以通过执行以下操作来使用此Java类:

<!--- Get the page. --->
<cfhttp method="get" url="http://example.com/" resolveurl="true" useragent="#cgi.http_user_agent#" result="myPage" timeout="10" charset="utf-8">
<cfhttpparam type="header" name="Accept-Encoding" value="*" />   
<cfhttpparam type="header" name="TE" value="deflate;q=0" />        
</cfhttp>

<!--- Load up jSoup and parse the document with it. --->
<cfset jsoup = createObject("java", "org.jsoup.Jsoup") />
<cfset document = jsoup.parse(myPage.filecontent) />

<!--- Search the parsed document for the contents of the TITLE tag. --->
<cfset title = document.select("title").first() />

<!--- Let's see what we got. --->
<cfdump var="#title#" />
Run Code Online (Sandbox Code Playgroud)

这个例子非常简单,但它可以告诉你使用它有多容易.如果你查看jSoup上的文档,那么刮图像和其他任何东西都会相当容易.

这个页面上有一些很好的例子,你可以使用CSS样式选择器:

http://jsoup.org/cookbook/extracting-data/selector-syntax

尽量避免使用正则表达式来完成这项任务 - 相信我,我已经尝试过,这是一个绝对的蠕虫!

希望这可以帮助.米奇.