0

我正在尝试softwareVersion从下面的 html 代码中获取。

<div class="title">Current Version</div> <div class="content" itemprop="softwareVersion"> 1.1.3  </div> </div> <div class="meta-info"> <div class="title">Requires Android</div> <div class="content" itemprop="operatingSystems">     2.2 and up   </div> </div>

我为此使用了以下代码

String Html = GetHtml("https://play.google.com/store/apps/details?id="+ AppID)
Pattern pattern = Pattern.compile("softwareVersion\">[^<]*</dd");
Matcher matcher = pattern.matcher(Html);
matcher.find();

String GetHtml(String url1) 
    {
        String str = "";
        try 
        {
            URL url = new URL(url1);
            URLConnection spoof = url.openConnection();
            spoof.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    spoof.getInputStream()));
            String strLine = "";
            // Loop through every line in the source
            while ((strLine = in.readLine()) != null) 
            {
                str = str + strLine;
            }
        } 
        catch (Exception e) 
        {
        }
        return str;
    }

但匹配器总是返回假。我认为我的模式有问题任何人都可以帮助我谢谢

4

1 回答 1

0

正如其他人所评论的那样,我通常会使用 html 解析器从 html 中提取内容。但是,在您只是从字符串中提取一点信息的情况下,我可以看到您为什么要使用正则表达式。

您需要做的是这样的事情-您的正则表达式的问题是额外的d. 此外,如果您将您关心的位括在括号中,您可以使用.group.

import java.util.regex.*;

public class R {

  public static void main(String[] args){
     String Html = "<div class=\"title\">Current Version</div> <div class=\"content\" itemprop=\"softwareVersion\"> 1.1.3  </div> </div> <div class=\"meta-info\"> <div class=\"title\">Requires Android</div> <div class=\"content\" itemprop=\"operatingSystems\">     2.2 and up   </div> </div>";

     Pattern pattern = Pattern.compile("softwareVersion\">([^<]*)</d");
     Matcher matcher = pattern.matcher(Html);
     System.out.println(matcher.find());
     System.out.println(matcher.group(1));
  }
}
于 2013-07-17T06:48:31.903 回答