簡體   English   中英

如何在Java中解析Zillow URL

[英]How to parse a zillow URL in java

我需要從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解析器https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html,但是我找不到找到“ zpid”的方法

您需要編寫一個正則表達式以匹配所需的組。 在您的情況下, 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;
}

您可以使用Integer.parseInt將此String為數字

這是您可以執行的操作:

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]);

            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM