gpt4 book ai didi

java - 从 HTML 页面中获取特定行并将其放入 String 中

转载 作者:太空宇宙 更新时间:2023-11-04 07:25:12 24 4
gpt4 key购买 nike

我正在尝试从此网页解析 html 的特定区域:

http://en.wikipedia.org/w/api.php?action=parse&page=Ringo_Starr&prop=text&section=0&format=txtfm&disablepp&redirects

[请注意,这不是源页面,它显示 html 标签,但我对此页面的实际源代码感兴趣 (Ctrl+u)]。

具体来说,我希望将所有以以下内容开头的行:

<span style="color:blue;">&lt;p&gt;</span>

转换为字符串。

enter image description here

这是我试图解决的问题 - 但我似乎还很遥远:

      Document doc = Jsoup.connect("http://en.wikipedia.org/w/api.php?action=parse&page=Ringo_Starr&prop=text&section=0&format=txtfm&disablepp&redirects").get();   
Elements elements = doc.select("span");
for (Element e : elements) {
if(e.text().equals("&lt;p&gt;")){
System.out.println("now get that whole line");
}
}

注意:我在这里使用 jsoup ——但是直接的正则表达式会更有效吗?

最佳答案

直接的正则表达式可能是一个更好的主意。首先尝试一下这个:

Pattern pat=Pattern.compile("^<span style=\"color:blue;\">&lt;p&gt;</span>.+&");

在这里,^开始行,<span style="color:blue;">&lt;p&gt;</span>从字面上匹配,我们就有一个或多个 non-line-terminator characters :

The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.

$指定行结束符。

关于java - 从 HTML 页面中获取特定行并将其放入 String 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18644203/

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