题记:
最近想着买房了,所以想从房天下上面爬点数据下来分析一下,看看买哪里比较好。。。 哈哈,开玩笑。
于是,用java写了一个爬虫,不过因为第一次写,并且只是一个demo,网上也借(chao)鉴(xi)了别人的一些代码,所以很多地方不是很完善,只是分享一下第一次写爬虫代码的一些心得体会,也为之后想学习爬虫的朋友提供一些参考,好了,下面进入正题。
我是分割线---------------------------------------------------------------------------------------------------------------------------
正题:大家都知道,要爬一个网站的数据,首先你就要了解这个网站,俗话说:知己知彼,百战不殆,我爬的是房天下成都的二手房数据,所以首先如下图所示,我大概把一个 内容对应的标签都给标记了出来,这是第一步,接着往后看。
Ps: 每个网站都是大同小异,其实大家不要把重心放在写代码上,个人觉得:分析网站 > 编写代码
好,网站分析过后才开始动手写代码了,我用的java语言,使用的是jsoup这个工具类,主要代码如下:
public List<BuildingInfo> getBuildInfoList() throws IOException {//Random r = new Random();List<IpProxy>ipList = getIpProxyList();int p=(int)(Math.random()*(ipList.size()-1));//int q = (int)(Math.random()*14);System.setProperty("https.proxySet", "true");Properties().put("https.proxyHost", (p).getIp_address());Properties().put("https.proxyPort", (p).getIp_port());logger.info("当前num="+p+",当前ip:"+(p).getIp_address()+":"+(p).getIp_port()+"n");// TODO Auto-generated method stub//boolean isStop = false;//Long startDate=System.currentTimeMillis();Document doc = null;//定义List装房屋信息List<BuildingInfo> buildList=new ArrayList<>();// Map<String, String> buildDateMap=new LinkedHashMap();// int num = 1;int num = 1;int j = 1;int count = 1;boolean judge = false;Date now = new Date();System.out.print("url:"+url+"n");for(int i = 63;i<=100;i++){//循环取数/*if(!judge){i = 1;}*/ if(i == 1){try{//userAgent(ua[j])doc = t(url).timeout(60000).method(Connection.Method.GET).followRedirects(true).get();judge = true;logger.info("第"+i+"页连接成功n");}catch (IOException e) {// e.printStackTrace();logger.info("当前ip不可用");// ve(num);logger.info("删除当前ip....进行下一次循环");num=(int)(Math.random()*(ipList.size()-1));j = (int)(Math.random()*14);System.setProperty("https.proxySet", "true");Properties().put("https.proxyHost", (num).getIp_address());Properties().put("https.proxyPort",
本文发布于:2024-02-02 04:34:55,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681972541389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |