Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取列表(视图)中的所有h4标记_Java_Android_Regex_List_Jsoup - Fatal编程技术网

Java 获取列表(视图)中的所有h4标记

Java 获取列表(视图)中的所有h4标记,java,android,regex,list,jsoup,Java,Android,Regex,List,Jsoup,我想从一个网站上获得一个特定div中的所有h4标签,并将它们全部放在一个列表(视图)中。 这是我尝试过的一点代码,但它不是jsoup,我不知道为什么,但它只从网站上每秒(或至少不是每秒钟)获取h4标记 Pattern firstNewsPattern = Pattern.compile("<h3><strong>Aktuelle Meldungen</strong></h3>(.*?)<hr />"); Pattern news

我想从一个网站上获得一个特定div中的所有h4标签,并将它们全部放在一个列表(视图)中。 这是我尝试过的一点代码,但它不是jsoup,我不知道为什么,但它只从网站上每秒(或至少不是每秒钟)获取h4标记

Pattern firstNewsPattern = Pattern.compile("<h3><strong>Aktuelle Meldungen</strong></h3>(.*?)<hr />");
    Pattern newsPattern = Pattern.compile("<hr />(.*?)<hr />");
    Pattern newsHeaderPattern = Pattern.compile("<h4>(.*?)</h4>");
    Pattern hrefPattern = Pattern.compile("href=\"(.*?)\"");
    Matcher newsHeader = null;
    Matcher href = null;

    Matcher firstNews = firstNewsPattern.matcher(html);
    if(firstNews.find()) {
        String content = firstNews.group(1).replace("./", "http://www.muckendorf-wipfing.at/");
        href = hrefPattern.matcher(content);
        while(href.find()) {
            String url = href.group(1);
            if(!url.contains("/")) {
                content = content.replace("href=\"" + url + "\"", "href=\"" + "http://www.muckendorf-wipfing.at/" + url + "\"");
            }
        }
        newsHeader = newsHeaderPattern.matcher(content);
        if(newsHeader.find())
            ret.add(new News(newsHeader.group(1).replaceAll("<(.*?)>", "").replaceAll("&#\\d{4};", ""), content));
    }

    Matcher news = newsPattern.matcher(html);
    while(news.find()) {
        String content = news.group(1).replace("./", "http://www.muckendorf-wipfing.at/");
        href = hrefPattern.matcher(content);
        while(href.find()) {
            String url = href.group(1);
            if(!url.contains("/")) {
                content = content.replace("href=\"" + url + "\"", "href=\"" + "http://www.muckendorf-wipfing.at/" + url + "\"");
            }
        }
        newsHeader = newsHeaderPattern.matcher(content);
        if(newsHeader.find())
            ret.add(new News(newsHeader.group(1).replaceAll("<(.*?)>", "").replaceAll("&#\\d{4};", ""), content));
    }
Pattern firstNewsPattern=Pattern.compile(Aktuelle-Meldungen(.*)
); Pattern newsPattern=Pattern.compile(“
(.*?
”); Pattern newsHeaderPattern=Pattern.compile((*?); 模式hrefPattern=Pattern.compile(“href=\”(.*?\”); Matcher newheader=null; Matcher href=null; Matcher firstNews=firstNewsPattern.Matcher(html); if(firstNews.find()){ String content=firstNews.group(1)。替换(“./”,”http://www.muckendorf-wipfing.at/"); href=hrefPattern.matcher(内容); while(href.find()){ 字符串url=href.group(1); 如果(!url.contains(“/”){ content=content.replace(“href=\”“+url+”\”“,“href=\”“+”http://www.muckendorf-wipfing.at/“+url+”\”); } } newsHeader=newsHeaderPattern.matcher(内容); if(newheader.find()) ret.add(新新闻(newheader.group(1).replaceAll(“,”).replaceAll(“&#\\d{4};”,”),内容)); } Matcher news=newsPattern.Matcher(html); while(news.find()){ 字符串内容=news.group(1)。替换(“./”,”http://www.muckendorf-wipfing.at/"); href=hrefPattern.matcher(内容); while(href.find()){ 字符串url=href.group(1); 如果(!url.contains(“/”){ content=content.replace(“href=\”“+url+”\”“,“href=\”“+”http://www.muckendorf-wipfing.at/“+url+”\”); } } newsHeader=newsHeaderPattern.matcher(内容); if(newheader.find()) ret.add(新新闻(newheader.group(1).replaceAll(“,”).replaceAll(“&#\\d{4};”,”),内容)); }
由于这段代码不是100%由我编写的,我甚至还没有完全理解它,所以我自己用jsoup再次编写了它,以使其正常工作:

List<News> ret = new ArrayList();
    getSharedPref sharedPrefMethod = new getSharedPref();
    SharedPreferences sharedPref = sharedPrefMethod.getSharedPref();
    String result = "";
    try {
        String pattern = "(\\<h3>\\.\\<h3>) (\\</h3>)";
        Pattern r = Pattern.compile(pattern);
        String html1 = html0.replace(Pattern.quote("<em>Taxigutscheine !NEU! (zum Vergrößern auf das Bild klicken)</em>"), Matcher.quoteReplacement("<h4>Neue Taxigutscheine!</h4>"));
        String html2 = html1.replace(Pattern.quote("<h3>"), Matcher.quoteReplacement("<h4>"));
        String html3 = html2.replace(Pattern.quote("</h3>"), Matcher.quoteReplacement("</h4>"));
        String html4 = html3.replaceFirst(Pattern.quote("<h4>"), Matcher.quoteReplacement("<h3>"));
        String finalHTML = html4.replaceFirst(Pattern.quote("</h4>"), Matcher.quoteReplacement("</h3>"));
        Matcher m = r.matcher(finalHTML);
        if (m.find()) {
        } else {
        }
        result = finalHTML.substring(finalHTML.indexOf("<h3>") + 4, finalHTML.indexOf("</h3>"));
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("AktuelleMeldungenHeadline", result);
        editor.commit();
    }catch(Exception e){

    }
    result = sharedPref.getString("AktuelleMeldungenHeadline", "");
    ret.add(new News(result, result));
List ret=new ArrayList();
getSharedPref sharedPrefMethod=新的getSharedPref();
SharedPreferences sharedPref=SharedPreferMethod.getSharedPref();
字符串结果=”;
试一试{
字符串模式=“(\\.\\)(\\)”;
Pattern r=Pattern.compile(Pattern);
字符串html1=html0.replace(Pattern.quote(“Taxigutscheine!NEU!(zum Vergröern auf das Bild klicken)”),Matcher.quoteReplacement(“Neue Taxigutscheine!”);
字符串html2=html1.replace(Pattern.quote(“”),Matcher.quoteReplacement(“”);
字符串html3=html2.replace(Pattern.quote(“”),Matcher.quoteReplacement(“”);
字符串html4=html3.replaceFirst(Pattern.quote(“”),Matcher.quoteReplacement(“”);
字符串finalHTML=html4.replaceFirst(Pattern.quote(“”),Matcher.quoteReplacement(“”);
匹配器m=r.Matcher(finalHTML);
if(m.find()){
}否则{
}
结果=finalHTML.substring(finalHTML.indexOf(“”+4,finalHTML.indexOf(“”));
SharedPreferences.Editor=sharedPref.edit();
编辑器.putString(“AktuelleMeldungenHeadline”,结果);
commit();
}捕获(例外e){
}
结果=sharedPref.getString(“AktuelleMeldungenHeadline”,”);
ret.add(新新闻(结果、结果));
有人能帮我解决一下,让它工作起来,这样我就能从div#content上得到每一个h3标签了吗?
谢谢

我认为你的低级代码有一些问题。。。如果你看2=1,3=2,4=3——你改变了从h3到h4的一切,然后立即又改变回来。。。事实上,我刚看到了替代者…@Nefarii是的,我有点搞砸了,我在这里坐了很长时间试图弄清楚这是怎么回事。。我要重写整件事,但我需要你们中的某个人的帮助,你能在这里帮我吗?你能发布一个示例进线吗?实际上是一条进线和一条终点线