java爬虫踩坑日记-房天下

本文记录了一位开发者使用java和jsoup爬取房天下成都二手房数据的过程,包括分析网页结构、解决重定向问题以及应对反爬策略。在实践中遇到的挑战,如获取区域信息和处理反爬机制,作者给出了相应的解决方案。
摘要由CSDN通过智能技术生成

题记:
最近想着买房了,所以想从房天下上面爬点数据下来分析一下,看看买哪里比较好。。。 哈哈,开玩笑。
在这里插入图片描述
于是,用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");
        System.getProperties().put("https.proxyHost", ipList.get(p).getIp_address());
        System.getProperties().put("https.proxyPort", ipList.get(p).getIp_port());
	  	  logger.info("当前num="+p+",当前ip:"+ipList.get(p).getIp_address()+":"+ipList.get(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 = Jsoup.connect(url).timeout(60000).method(Connection.Method.GET).followRedirects(true).get();
  	  				judge = true;
  	  				logger.info("第"+i+"页连接成功\n");
  	  			}catch (IOException e) {
   
			       // e.printStackTrace();
			        logger.info("当前ip不可用");
			       // ipList.remove(num);
			        logger.info("删除当前ip....进行下一次循环");
			        num=(int)(Math.random()*(ipList.size()-1));
		      	  	  j = (int)(Math.random()*14);
		              System.setProperty("https.proxySet", "true");
		              System.getProperties().put("https.proxyHost", ipList.get(num).getIp_address());
		              System.getProperties().put("https.proxyPort", ipList.get(num)<
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值