java爬虫(java访问url通过代理和jsoup抓取网页数据)

直接上代码:

	  public String getpricebyinternet(String href,Rfq_Detail record,HttpServletRequest request){
		  String Path = request.getSession().getServletContext().getRealPath("/WEB-INF/file/"+"proxy.properties");//获取代理资源文件路径
		  double minprice=10.0;
		  FileInputStream in = null; 
		  try {
			  Properties properties = new Properties(); //实例Properties对象
				in = new FileInputStream(Path);//读取资源文件
				properties.load(in);//加载资源文件
				
				String status=properties.getProperty("status");//获取资源文件内容
				if (status.equals("true")) {
					String ip=properties.getProperty("ip");//获取资源文件中名为IP的值
					int port=Integer.valueOf(properties.getProperty("port"));//获取资源文件中名为port的值
					String username=properties.getProperty("username");//获取资源文件中名为username的值
					String password=properties.getProperty("password");//获取资源文件中名为password的值
					Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));//设置代理,如不需代理可注释
			    	Authenticator.setDefault(new BasicAuthenticator(username, password));//设置代理用户名和密码,如不需可注释
			    	URL url = new URL(href);//实例化URL,href为要抓取网站的地址  
			    	HttpURLConnection urlcon = (HttpURLConnection)url.openConnection(proxy);  //打开连接
			    	urlcon.connect();  
			    	
				
				//获取连接  
			    	InputStream is = urlcon.getInputStream();  
			    	BufferedReader buffer = new BufferedReader(new InputStreamReader(is));  
			    	StringBuffer bs = new StringBuffer();  
			    	String l = null;  
			    	while((l=buffer.readLine())!=null){  
			    	bs.append(l);  
			    	}  	      
			    	Document doc = Jsoup.parse(bs.toString());	//jsoup    	
			    	Element div = doc.getElementById("list1588");	    	
			    	Elements table=div.select("table");   	
			    	Elements tbody=table.select("tbody");
			    	
			    	// 使用选择器选择该table内所有的<tr> <tr/>
			    	Elements trs = tbody.select("tr");
			    	//遍历该表格内的所有的<tr> <tr/>
			   
			    	for (int i = 0; i < trs.size(); ++i) {
			    		// 获取一个tr
			    		Element tr = trs.get(i);
			    		// 获取该行的所有td节点
			    		Elements tds = tr.select("td");
			    		// 选择某一个td节点
			    		for (int j = 0; j < tds.size(); ++j) {
			    			Element td = tds.get(j);
			    			// 获取td节点的所有li
			    			Elements lis = td.select("li");
			    			// 选择一个div
			    			for (int k = 0; k < lis.size(); k++) {	    				
			    				Element li = lis.get(k);
			    				//获取文本信息
			    				String text = li.text();	    				
			    				if (!li.text().equals("See More")) {
			    					double text2=Double.valueOf(text.substring(text.indexOf("$")+1));
			    					
			    					if (text2<minprice) {
			    						minprice=text2;
									}	    						    						    					    					
								}	    							
			    		}
			    	}
			    	}
			    	
			    	String minprice2="$".concat(String.valueOf(minprice));	    	
			    	
					
				}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		  return minprice2;
	  }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值