我们目前有一些文章发布到我们的网站上。它们可以与以下类型的 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>
或
<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>
有时其他一些html标签可能在其中,我无法理解如何使用coldfusion来抓取页面来实现这一点
基本上我需要做的是捕获第一段文字和图像并能够安排它。
这可能使用 Coldfusion 8 吗?任何人都可以为我指出如何学习这个的方向吗?
最佳答案
100% 绝对有可能!
现在,不要被我的建议所拖延,这实际上很容易上手。
下载一个名为 jSoup 的库...它的唯一目的是从网页中的 DOM 中抓取内容:
然后您可以通过执行以下操作来使用这个 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#" />
这个例子很简单,但它可以告诉你它是多么容易使用。如果您查看 jSoup 上的文档,那么抓取图像和其他任何事情都会相当容易。
此页面上有一些很好的示例,您可以在其中使用 CSS 样式选择器:
http://jsoup.org/cookbook/extracting-data/selector-syntax
尽量避免在这项任务中使用正则表达式 - 相信我,我已经尝试过了,这绝对是一 jar 蠕虫!
希望这会有所帮助。 米奇。
关于regex - Coldfusion - 简单的 HTML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16354818/