【问题标题】:Jsoup Absolute PathJsoup 绝对路径
【发布时间】:2020-08-25 01:13:16
【问题描述】:

我正在努力获取从我的网站上抓取的图像的绝对路径。我查看了 jsoup.org 上的文档,但我无法让 abs:src 工作。我不知道如何实现 abs:src 或在哪里添加它。

<cfhttp method="get" url="https://theculturecook.com/recipe-slowroasted-pork-belly.html" result="theresult">        
<cfscript>
    Jsoup = createObject("java", "org.jsoup.Jsoup");
    html = "#theresult.filecontent#";
    doc = Jsoup.parse(html);
    tags = doc.select("img[src$=.jpg]");
</cfscript>
<cfset images = "">
<cfloop index="e" array="#tags#">
    <cfoutput>
       <cfset images = ListAppend(images,#e.attr("src")#)>
    </cfoutput>
</cfloop>
<cfloop list="#images#" index="a">
    <cfoutput>#a#<br></cfoutput>
</cfloop>

【问题讨论】:

标签: parsing coldfusion jsoup


【解决方案1】:

您面临的问题是将 html 内容传递给 JSOUP。如果你需要绝对路径,那么你需要使用跟随来连接。

Jsoup.connect("https://theculturecook.com/recipe-slowroasted-pork-belly.html").get();

最后,

<cfscript>
    Jsoup = createObject("java", "org.jsoup.Jsoup");
    doc = Jsoup.connect("https://theculturecook.com/recipe-slowroasted-pork-belly.html").get();
    tags = doc.select("img[src$=.jpg]");
</cfscript>
<!--- <cfdump var="#a.attr()#" abort> --->
<cfset images = "">
<cfloop index="e" array="#tags#">
    <cfoutput>
       <cfset images = ListAppend(images, e.attr("abs:src"))>
    </cfoutput>
</cfloop>
<cfloop list="#images#" index="a">
    <cfoutput>#a#<br></cfoutput>
</cfloop>

【讨论】:

  • 好点。 JSoup 需要一个“基本”url 来解析内容中的相对路径。使用 JSoup 直接获取 URL 提供了该上下文。也可以使用重载方法手动完成:JSoup.parse( html, baseURL)
猜你喜欢
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2010-09-15
  • 2013-04-17
  • 1970-01-01
  • 2012-10-16
相关资源
最近更新 更多