JAVA爬虫Jsoup,抓取房价
生活随笔
收集整理的這篇文章主要介紹了
JAVA爬虫Jsoup,抓取房价
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
里面的命名很爛,但能跑出來效果。
依賴:
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.10.3</version> </dependency> package cn.temptation.web;import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern;import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements;public class fangjia {public static void main(String[] args) throws IOException {Document doc = Jsoup.connect("https://wuz.fang.anjuke.com/").header("Accept-Encoding", "gzip, deflate").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0").maxBodySize(0).timeout(600000).get();Elements xiaoqu=doc.select(".items-name");Elements jiage=doc.select(".price");//System.out.println(xiaoqu);//System.out.println(jiage);StringBuffer sb=new StringBuffer();sb.append(xiaoqu.toString());String str = sb.toString();StringBuffer sb1=new StringBuffer();sb1.append(jiage.toString());String str1 = sb1.toString();//使用正則表達(dá)式Pattern pattern = Pattern.compile("[^\u4E00-\u9FA5]");//[\u4E00-\u9FA5]是unicode2的中文區(qū)間Matcher matcher = pattern.matcher(str);//System.out.println(matcher.replaceAll(" "));String sd=matcher.replaceAll(" ");String[] xiqoushuzu =sd.split(" ");List <String> list = new ArrayList<String>();for (int i = 0; i <xiqoushuzu.length; i++) { if (xiqoushuzu[i]!=null&&!xiqoushuzu[i].equals("")){ list.add(xiqoushuzu[i]);} } for(String attribute : list) {//System.out.println(attribute);} //list 是所有小區(qū)的名字//使用正則表達(dá)式Pattern pattern1 = Pattern.compile("\\D");//[\u4E00-\u9FA5]是unicode2的中文區(qū)間Matcher matcher1 = pattern1.matcher(str1);//System.out.println(matcher.replaceAll(" "));String jg=matcher1.replaceAll(" ");String[] fangjia =jg.split(" ");List <String> list1 = new ArrayList<String>();for (int i = 0; i <fangjia.length; i++) { if (fangjia[i]!=null&&!fangjia[i].equals("")){ list1.add(fangjia[i]);} } List<Integer> IntegerList = new ArrayList<Integer>();for (String x : list1) {Integer z = Integer.parseInt(x);IntegerList.add(z);} for(int i = 0 ; i < IntegerList.size() ; i++) {if(IntegerList.get(i)<1000) {IntegerList.remove(i);}}int total = 0;for(int i = 0 ; i < IntegerList.size() ; i++) {total += IntegerList.get(i);}int avg = total/IntegerList.size();System.out.println("吳忠市平均房?jī)r(jià)是:"+avg);//list 是所有小區(qū)的價(jià)格}}?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的JAVA爬虫Jsoup,抓取房价的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天天跳绳如何投屏电视(14岁天天早恋引争
- 下一篇: mybatis和hibernate的一级