gpt4 book ai didi

java - 使用java从HTML中提取数据

转载 作者:行者123 更新时间:2023-11-29 03:32:36 26 4
gpt4 key购买 nike

我想使用 Java 提取数据 HTML。我尝试使用 Jsoup,但到目前为止我无法提取正确的数据。这是我试图从中提取数据的 HTML 代码片段。

<a href="javascript:;" id="listen_880966" onclick="MP3PREVIEWPLAYER.showHiddePlayer(880966, 'http://mksh.free.fr/' + 'lol/mp3/Paint_It_Black/18_the_black_dahlia_murder_-_paint_it_black_(rolling_stones)-bfhmp3.mp3')" title="Listen Paint it Black    The Black Dahlia Murder   Great Metal Covers 36" class="button button-s button-1 listen "   >

我想将链接 ("http://mksh.free.fr/ ' + 'lol/mp3/Paint_It_Black/18_the_black_dahlia_murder_-_paint_it_black_(rolling_stones)-bfhmp3.mp3") 和标题提取到不同的变量中。如果随答案一起提供示例代码,那将非常有帮助。

最佳答案

您可以使用正则表达式来解析出您想要的部分。然后你可以使用类似 string.split(delimiter) 的东西来提取特定信息。参见 this link有关 string.split() 方法的信息

import java.util.regex.*;
import java.lang.*;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String mydata = "<a href=\"javascript:;\" id=\"listen_880966\" onclick=\"MP3PREVIEWPLAYER.showHiddePlayer(880966, 'http://mksh.free.fr/' + 'lol/mp3/Paint_It_Black/18_the_black_dahlia_murder_-_paint_it_black_(rolling_stones)-bfhmp3.mp3')\" title=\"Listen Paint it Black The Black Dahlia Murder Great Metal Covers 36\" class=\"button button-s button-1 listen \" >";
Pattern pattern = Pattern.compile("'http://mksh.free.fr/'\\s.\\s'[\\(\\).A-Za-z0-9/_-]+'");
Pattern title = Pattern.compile("title=\\\"[A-Za-z0-9\\s]+\\\"");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.group(0));

}
matcher = title.matcher(mydata);
if(matcher.find())
System.out.println(matcher.group(0));
}
}

Ideone

关于java - 使用java从HTML中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17360411/

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