我正在尝试从此网页解析 html 的特定区域:
http://en.wikipedia.org/w/api.php?action=parse&page=Ringo_Starr&prop=text§ion=0&format=txtfm&disablepp&redirects
[请注意,这不是源页面,它显示 html 标签,但我对此页面的实际源代码感兴趣 (Ctrl+u)]。
具体来说,我希望将所有以以下内容开头的行:
<span style="color:blue;"><p></span>
转换为字符串。
这是我试图解决的问题 - 但我似乎还很遥远:
Document doc = Jsoup.connect("http://en.wikipedia.org/w/api.php?action=parse&page=Ringo_Starr&prop=text§ion=0&format=txtfm&disablepp&redirects").get();
Elements elements = doc.select("span");
for (Element e : elements) {
if(e.text().equals("<p>")){
System.out.println("now get that whole line");
}
}
注意:我在这里使用 jsoup ——但是直接的正则表达式会更有效吗?
最佳答案
直接的正则表达式可能是一个更好的主意。首先尝试一下这个:
Pattern pat=Pattern.compile("^<span style=\"color:blue;\"><p></span>.+&");
在这里,^
开始行,<span style="color:blue;"><p></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/