0

我需要从 URL 中获取“zpid”,例如参见以下链接: http ://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,- 158.027859_rect/12_zm/

我需要得到值 110560800

我找到了 URL Parser https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html 但我找不到获取“zpid”的方法

4

2 回答 2

0

您需要编写一个正则表达式来匹配您想要的组。在您的情况下,是一个与要使用zpid的数字匹配的数字\d+

private static String extract(String url) { // http://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,-158.027859_rect/12_zm/
    Pattern pattern = Pattern.compile("(\\d+)_zpid");
    Matcher matcher = pattern.matcher(url);
    while (matcher.find()) {
        return matcher.group(1); //110560800
    }
    return null;
}

您可以String使用Integer.parseInt

于 2016-10-26T03:25:29.110 回答
0

您可以这样做:

String s = "http://www.zillow.com/homes/for_sale/Laie-HI/110560800_zpid/18901_rid/pricea_sort/21.70624,-157.843323,21.565342,-158.027859_rect/12_zm/";

        String[] url = s.split("/");//separating the string with delimeter "/" in url

        for(int i=0;i<url.length;i++){
            if(url[i].contains("zpid")){//go through each slit strings and search for keyword zpid
                String[] zpid = url[i].split("_");//if zpid is found, get the number part
                System.out.println(zpid[0]);

            }
        }
于 2016-10-26T03:25:58.957 回答