java 学习:网络爬虫--中国人才热线邮箱抓取

很简陋的一个抓取邮箱的,抓取效率很低,纯当熟悉键盘。

1. 函数入口

public class Test01 {
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args)  {	
		for (int i=1; i<=20; i++) {
			HtmlPage h1 = new HtmlPage(	    
					"http://www.cjol.com/search/l2008/"+i+"/?Keyword=%E5%A4%96%E8%B4%B8%E4%B8%9A%E5%8A%A1%E5%91%98&KeywordType=3&RecentSelected=43",1);
			h1.pageCode();
			//new Thread(new mRunable(h1), ""+i).start();
		}
		//http://www.cjol.com/search/l2008/4/?Keyword=%E5%A4%96%E8%B4%B8%E4%B8%9A%E5%8A%A1%E5%91%98&KeywordType=3&RecentSelected=43
	//	h1.email();
		//h1.pageCode();
		// h1.email();
	}	
}

2. 正则表达式

public class Regx {
	/**
	 * 搜索业务员找公司页面 pat = "href=\"http://[\\w-\\./]+\">[\u4e00-\u9fa5]*有限公司</a>"
	 * 
	 * @param buf
	 * @throws IOException
	 */
	public synchronized static void findCompany(String buf) {
		List<String> companyList = new ArrayList<String>();
		// System.out.println("resource:"+buf);
		// System.out.println("findCompany()");
		Pattern pattern = Pattern // \\s*target=\"_blank\"
				.compile("href=\"http://[\\w-\\./]+\">[\u4e00-\u9fa5]*有限公司</a>");
		Matcher matcher = pattern.matcher(buf);
		Pattern innerPattern = Pattern.compile("http:\\S+\"");

		while (matcher.find()) {
			String string = matcher.group();
			// System.out.println(string);
			Matcher innerMatcher = innerPattern.matcher(string);
			if (innerMatcher.find()) {
				String tmp = innerMatcher.group().replaceAll("\"", "");
				new HtmlPage(tmp, 2);
				String ttString = "公司招聘页面地址:" + tmp;
				System.out.println(ttString);
				HtmlPage.writLog(ttString);
			}

			companyList.add(string);
		}
	}

	/**
	 * 找到公司官网主页地址
	 * 
	 * @param buf
	 * @throws IOException
	 */
	public synchronized static void findWebSite(String buf) {

		List<String> webSiteList = new ArrayList<String>();
		Pattern pattern = Pattern.compile("网址:<a href=\"http://[\\w-\\./]+\"");
		Matcher matcher = pattern.matcher(buf);

		Pattern innerPattern = Pattern.compile("http:\\S+\"");
		while (matcher.find()) {
			String string = matcher.group();
			// System.out.println("找到啦:"+string);
			Matcher innerMatcher = innerPattern.matcher(string);
			if (innerMatcher.find()) {
				String tmp = innerMatcher.group().replaceAll("\"", "");
				String ttsString = "公司主页地址:" + tmp;
				System.out.println(ttsString);
				HtmlPage.writLog(ttsString);
				new HtmlPage(tmp, 3);
			}
			webSiteList.add(string);
		}
	}

	/**
	 * 在官网主页找 联系我们/contact us <a href="contactus.asp">联系方式</a>
	 * href="contact.php">CONTACT US</a>
	 * 
	 * @param str
	 * @throws IOException
	 */
	public synchronized static void findContanct(String url, String str) {

		List<String> webSiteList = new ArrayList<String>();
		Pattern pattern = Pattern.compile("href=\"[\\w-\\./\\?=]+\">contact",
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		Pattern innerPattern = Pattern.compile("\".+\"");

		while (matcher.find()) {
			String string = matcher.group();
			String ttsString = "联系方式地址:" + string;
			System.out.println(ttsString);
			HtmlPage.writLog(ttsString);
			Matcher innerMatcher = innerPattern.matcher(string);
			if (innerMatcher.find()) {
				String tmp = innerMatcher.group().replaceAll("\"", "");
				new HtmlPage(url + "//" + tmp, 4);
			}
			webSiteList.add(string);
		}
	}

	/**
	 * 邮箱地址验证
	 * 
	 * @param str
	 * @return
	 */
	public synchronized static List<String> email(String str) {
		File file = new File("1.txt");
		RandomAccessFile rd = null;
		try {
			rd = new RandomAccessFile(file, "rw");
			rd.seek(file.length());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Pattern pattern = Pattern
				.compile("[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+\\.[a-zA-Z]{2,4}");
		Matcher matcher = pattern.matcher(str);
		List<String> list = new ArrayList<String>();
		while (matcher.find()) {
			String reString = matcher.group();
			
			
			if (HtmlPage.putEmail(reString)) {
				System.out
				.println("邮箱:------------------------------------------------------------------------- "
						+ reString + "---------------");
				HtmlPage.writLog("邮箱: "+reString);
				try {

					rd.write(reString.getBytes());
					rd.write("\r\n".getBytes());
				} catch (IOException e) {
					HtmlPage.writLog(reString+" 写邮箱失败:"+e.getMessage());
					System.out.println("邮箱写入失败:"+e.getMessage());
					e.printStackTrace();
				} finally {
					try {
						if (rd!= null) 
							rd.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				list.add(reString);
				return list;
			}
		}
		return null;
	}
}

3. 页面内容抓取

public class HtmlPage {
	private String spec;
	private int depth;
	
	// private String pageCode;
	private static List<String> emailList = new ArrayList<String>();

	public HtmlPage(String urlString, int depth)  {
		this.spec = urlString;
		this.depth = depth;
		System.out.println("---------"+urlString +"----"+ depth);
		HtmlPage.writLog("---------"+urlString +"----"+ depth);
		if (depth !=1)
			pageCode();
	}

	public void pageCode()  {
		URL url = null;
		try {
			url = new URL(spec);
		} catch (MalformedURLException e) {
			HtmlPage.writLog(spec+" 初始化失败:"+e.getMessage());
			System.out.println("url初始化失败");
			e.printStackTrace();
			return;
		}
		StringBuffer sBuffer = new StringBuffer();
		HttpURLConnection connection;
		try {
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		} catch (IOException e) {
			HtmlPage.writLog(spec+" 打开网址失败:"+e.getMessage());
			System.out.println("打开网址失败");
			e.printStackTrace();
			return;
		}
		connection.setDoOutput(true);
		// 网页编码
		//
		String charset = getCharset(connection.getContentType());
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), charset));
		} catch (UnsupportedEncodingException e) {
			HtmlPage.writLog(spec+" 初始化输入流:"+e.getMessage());
			e.printStackTrace();
			return;
		} catch (IOException e) {
			HtmlPage.writLog(spec+" 初始化输入流:"+e.getMessage());
			e.printStackTrace();
			return;
		}
		String str = null;
		try {
			while ((str = br.readLine()) != null) {
				switch (depth) {
				case 1:
					Regx.findCompany(str);
					break;
				case 2:
					Regx.findWebSite(str);
					break;
				case 3:
					Regx.findContanct(spec, str);
					Regx.email(str);
					break;
				case 4:
					Regx.email(str);
					break;
				default:
					break;
				}

			}
		} catch (IOException e) {
			HtmlPage.writLog(spec+" 读取输入流:"+e.getMessage());
			System.out.println(e.getMessage());
			//e.printStackTrace();
			return;
		}
	}

	/**
	 * 网页编码
	 * 
	 * @param contentType
	 * @return
	 */
	private String getCharset(String contentType) {
		if (contentType == null)
			return "gbk";
		Pattern pattern = Pattern.compile("charset=.*");
		Matcher matcher = pattern.matcher(contentType);
		if (matcher.find())
			return matcher.group(0).split("charset=")[1];
		return "gbk";
	}

	public synchronized static boolean putEmail(String str) {
		if (!emailList.contains(str)) {
			emailList.add(str);
			return true;
		}
		return false;
	}
	public synchronized static void writLog(String str) {
		File file = new File("log.txt");
		RandomAccessFile rd = null;
		try {
			 rd = new RandomAccessFile(file, "rw");
			int len = (int) file.length();
			rd.seek(len);
			rd.write(str.getBytes());
			rd.write("\r\n".getBytes());
		} catch (FileNotFoundException e) {
			System.out.println("日志写入失败!");
			e.printStackTrace();
			
		} catch (IOException e) {
			System.out.println("日志写入失败!");
			e.printStackTrace();
		} finally {
			try {
				if (rd!= null) 
					rd.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void email() {
		int len = emailList.size();
		for (int i=0; i<len; i++) {
			System.out.println(emailList.get(i));
		}
	}
}


4. 多线程

public class mRunable implements Runnable {

	private HtmlPage htmlPage;
	public mRunable() {
	}
	
	public mRunable(HtmlPage htmlPage) {
		this.htmlPage = htmlPage;
	}

	@Override
	public void run() {
		System.out.println("\n\n线程---------------------------------------------------- ----"+Thread.currentThread().getName() +"滴滴开始了啦----------\n\n\n");
		HtmlPage.writLog("线程"+Thread.currentThread().getName()+"开始运行");
		htmlPage.pageCode();
		System.out.println("\n\n线程----------------------------------------------------  ----"+Thread.currentThread().getName() +"完成工作啦----------\n\n\n");
		HtmlPage.writLog("线程"+Thread.currentThread().getName()+"运行结束");
	}
}


内容概要:本文围绕“考虑电能交互的冷热电区域多微网系统双层多场景协同优化配置”的Matlab代码实现展开,提出一种结合电能交互机制的双层优化模型,用于解决冷、热、电多能耦合背景下多微网系统的协同规划与运行问题。研究采用多场景分析方法应对可再生能源出力与负荷需求的不确定性,通过上层规划设备容量配置与下层优化多时段运行策略的联动,提升系统在复杂环境下的经济性、鲁棒性与能源利用效率。所提供的Matlab代码集成了建模、求解(如YALMIP+CPLEX)与结果可视化全流程,涵盖场景生成与削减、双层优化结构设计及多能流协同调度等关键技术环节,为综合能源系统优化提供了完整的算法实现与技术参考。; 适合人群:具备电力系统、综合能源系统或优化建模背景,熟悉Matlab编程与数学规划方法,正在从事相关领域科研或工程设计工作的研究生、高校研究人员及能源行业技术人员。; 使用场景及目标:①开展冷热电联供(CCHP)多微网系统的容量规划与运行优化研究;②支撑含分布式能源、储能及多能转换设备的综合能源系统多目标、多场景优化建模;③学习与复现双层优化、分布鲁棒优化及场景分析等先进优化方法在能源系统中的实际应用。; 阅读建议:建议结合配套文献与代码同步研读,重点理解双层模型的构建逻辑、变量耦合关系与求解技巧,关注场景生成方法与YALMIP调用细节,通过调整参数、修改目标函数等方式进行仿真实验,以深化对系统优化机理的掌握。
内容概要:本文系统研究了单相逆变器闭环控制下的PWM调制模型,基于Simulink平台构建完整的逆变电路仿真系统,涵盖主电路拓扑、闭环控制器设计、脉宽调制信号生成及输出滤波等关键环节。通过引入比例积分(PI)反馈控制策略,实现对输出电压幅值与波形的精确调节,有效抑制负载扰动带来的影响,提升系统的动态响应能力与稳态精度。仿真过程详细展示了系统建模、参数整定及性能验证的全流程,重点分析了闭环控制在改善输出正弦波质量、降低谐波畸变率方面的优势,为电力电子逆变装置的研发与优化提供了可靠的理论支撑与实践参考。; 适合人群:具备电力电子技术、自动控制原理基础知识及相关仿真经验的高校研究生、科研人员,以及从事新能源发电、不间断电源(UPS)、微电网、电动汽车等领域的工程技术人员。; 使用场景及目标:①掌握单相逆变器闭环控制系统的设计与建模方法;②深入理解PWM技术与反馈控制在逆变系统中的协同工作机制;③通过Simulink仿真平台完成系统搭建与参数调试,服务于课程设计、毕业课题、科研项目或工业产品开发中的逆变器控制算法验证。; 阅读建议:建议结合经典控制理论与电力电子变换技术同步学习,动手复现仿真模型并尝试调整PI控制器参数、载波频率等关键变量,观察其对系统稳定性与输出性能的影响,从而深化对控制机理的理解,并为进一步研究并网逆变、多电平逆变等复杂系统打下坚实基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值