如何在 java 中解析 zillow URL
How to parse a zillow URL in java
我需要从 URL 中获取 "zpid" 例如,请参阅以下内容 link:
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]);
}
}
我需要从 URL 中获取 "zpid" 例如,请参阅以下内容 link: 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]);
}
}