获取网页中所有的小客车摇号信息

本文介绍了一种方法,通过编写代码批量下载杭州摇号网站的数据,避免了分页显示带来的不便。

    杭州摇号网站分页显示,浏览起来非常的不方便,于是写了几行代码

将它给全部下载到一个文件中。代码很简单,先通过httpwatch观察,http

是如何拼接请求的,然后在代码中将链接拼接好,批量下载下来。

  

PrintStream ps = null;
		try {
			ps = new PrintStream(new FileOutputStream("company.txt"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		for(int i = 1; i <= 40; i++){
			HttpClient client = new DefaultHttpClient();
			//http://apply.hzcb.gov.cn/apply/app/status/norm/unit
			//http://apply.hzcb.gov.cn/apply/app/status/norm/person
			HttpPost httppost = new HttpPost("http://apply.hzcb.gov.cn/apply/app/status/norm/unit");
			httppost.setHeader("Referer","http://apply.hzcb.gov.cn/apply/app/status/norm/unit");
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			String pageNo = new Integer(i).toString();
			params.add(new BasicNameValuePair("pageNo", pageNo));
			params.add(new BasicNameValuePair("issueNumber", "201405"));
			params.add(new BasicNameValuePair("applyCode", ""));
			httppost.setEntity(new UrlEncodedFormEntity(params));

			HttpResponse response = client.execute(httppost);
			HttpEntity  entity = response.getEntity();
			// 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断登录是否成功
			String postResult = EntityUtils.toString(entity, "GBK"); 
			Document document = Jsoup.parse(postResult);
			Elements carpersons = document.select("tr.content_data");
			for(Element e : carpersons){
				String id = "";
				String name = "";
				Elements props = e.select("td");
				int count = 0;
				for(Element e1 : props){
					if(count == 0)
						id = e1.text();
					else
						name = e1.text();
					count++;
				}
				System.out.println("id:" + id + " name:" + name);
				ps.println(id + "\t" + name);
				
			}
		}
		if (null != ps) {
			ps.close();
		}
	}

 

转载于:https://www.cnblogs.com/cstar/p/3778168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值