gpt4 book ai didi

java - 如何从 html 文本中获取 img url

转载 作者:行者123 更新时间:2023-11-30 01:29:11 24 4
gpt4 key购买 nike

在Android开发中,如何从如下所示的HTML文本中获取图片URL或加载图片?我从 HTML 中获取它,我想从这段代码中获取图像 URL:

<p style="text-align: justify;"><span style="font-size: 16px;"><img class="aligncenter wp-image-2699 size-large" src="http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-1024x258.jpg" alt="helal parti-1" width="750" height="189" srcset="http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-300x76.jpg 300w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-768x193.jpg 768w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-1024x258.jpg 1024w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-560x141.jpg 560w, http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1.jpg 1564w" sizes="(max-width: 750px) 100vw, 750px" /></span></p>

我想获取 src="http://www.helalsaglikliyasam.org/wp-content/uploads/2016/02/helal-parti-1-1024x258.jpg"

最佳答案

您可能应该使用 HTML 解析器——有几个 Java HTML 解析库可以通过快速搜索找到。

然而,一种快速而肮脏的方法是在输入字符串中搜索 src=" 声明,如下所示:

int index = input.indexOf("src=\"");
String substr = input.substring(index + 5);
int endIndex = substr.indexOf("\"");
String imgUrl = substr.substring(0, endIndex);

免责声明:我还没有测试过这个,所以它可能有错误。它还做出了很多可能不正确的假设——这就是为什么你应该为这类事情使用库!

编辑:修复了一个测试后的错误(必须使用与我正在输入的机器不同的机器)。它现在应该适合您了——但同样,您真的应该使用一个库。

关于java - 如何从 html 文本中获取 img url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com