资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
JAVA开发一个简单的爬虫的思路1使用Get方法请求网页数据公司销售部门需求:需要将网站http:/company.ef360.com/按地区,将所有的服装企业数据保存到Excel里去。网络上有一堆通用的或是专用的爬虫,我一直也想去写一个通用的。两年前,同事培训过一个叫htmlparser 的,这几天和一个网友聊天,他推荐给我一些也都蛮不错的。开始,准备工具:Eclipse 3.4.2 (下载地址:http:/www.eclipse.org/downloads/)Jdk 1.6.7 (下载地址:http:/developers.sun.com/downloads/)Httpclient 3.1 相关jar包 (下载地址:http:/hc.apache.org/downloads.cgi)在写本文之前,所有使用的版本均为次新版,保证稳定性能,对应的最新版分别为:Eclipse 3.5.1Jdk 1.7HttpClient 4.1解压eclipse3.4.2,安装jdk1.6.7,配置eclipse,设置eclipse使用的java虚拟机。如图:在eclipse菜单Window-Perferences中的Installed JREs新建工程新建普通的java工程,一切默认新建libs目录,将相关的包,加入到编译环境第一个jar包,是一个复合的包,将apache.commons的相关包打进去,暂时用到orgapachepoi中的相关,可以到本文后面提供的下载路径下载工程。用apache poi包代替即可。Jar包找不到,可以到这里下载http:/www.java2s.com/Code/Jar/CatalogJar.htm选中这4个包,右键,build path-add to build path,这样,程序可以识别到外部引用的包新建带Main()的类Ef360Main.javaimport java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Ef360Main private static final String LOGON_SITE = http:/www.ef360.com;private static final int LOGON_PORT = 80;private Log log = LogFactory.getLog(this.getClass();/用来写excel中的头描述private static String desc=new String公司介绍,主营产品,公司名称,所在省市,公司地址,邮编,联系人,联系电话,手机号码,联系传真,网址,公司网址;/用来写excel的头描述字段private static String key=new StringcompanyInfo,products,companyName,province,address,zipCode,headerMan,phone,mobilePhone,tax,detailUrl,netUrl;public static void main(String args) throws HttpException, IOException HttpClient client = new HttpClient();client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);/ 地区编号String areaIds = 350000;int pages = 200;/每行的结果List resultList=new ArrayList();/已经添加的过滤Map duclipcateCode=new HashMap();for (int i = 100; i 1) baseUrl+=&page=+i;GetMethod get = new GetMethod(baseUrl);get.getParams().setContentCharset(GB2312);client.executeMethod(get);String responseString = get.getResponseBodyAsString();get.releaseConnection();/ 处理登录后首页的内容Matcher m = Pattern.compile(Detail/.*html).matcher(responseString);while (m.find() Matcher m2 = Pattern.compile(0-9+).matcher(m.group();if (m2.find() String findCode=m2.group();if(duclipcateCode.get(findCode)!=null) continue;duclipcateCode.put(findCode, 1);String detailUrl = http:/company.ef360.com/Detail/+ findCode + .html;Map result=new HashMap();System.out.println(访问子项:+detailUrl);/公司介绍String companyInfo=;/主营产品String products=;/公司名称String companyName=;/所在省市String province=;/公司地址String address=;/邮编String zipCode=;/联系人String headerMan=;/联系电话String phone=;/手机号码String mobilePhone=;/联系传真String tax=;/公司网址String netUrl=;/取每一个公司的详细GetMethod get2 = new GetMethod(detailUrl);get2.getParams().setContentCharset(GB2312);client.executeMethod(get2);String detailStrContent = get2.getResponseBodyAsString();get2.releaseConnection();companyInfo=processDetail(detailStrContent,公司介绍sS*CompanyTit_Body.*,CompanyTit_Body,null,2,0);products=processDetail(detailStrContent,主营产品sS*InnerContent_Search.*,InnerContent_Search,3,0);companyName=processDetail(detailStrContent,公司名称sS*InnerContent_Search.*,InnerContent_Search,3,0);province=processDetail(detailStrContent,所在省市sS*InnerContent_Search.*,InnerContent_Search,3,0);address=processDetail(detailStrContent,公司地址sS*InnerContent_Search.*,InnerContent_Search,3,0);zipCode=processDetail(detailStrContent,邮编sS*InnerContent_Search.*,InnerContent_Search,3,0);headerMan=processDetail(detailStrContent,联系人sS*InnerContent_Search.*,InnerContent_Search,3,0);phone=processDetail(detailStrContent,联系电话sS*InnerContent_Search.*,InnerContent_Search
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号