JAVA开发一个简单的爬虫的思路1

上传人:鲁** 文档编号:512045247 上传时间:2022-08-17 格式:DOC 页数:10 大小:177KB
返回 下载 相关 举报
JAVA开发一个简单的爬虫的思路1_第1页
第1页 / 共10页
JAVA开发一个简单的爬虫的思路1_第2页
第2页 / 共10页
JAVA开发一个简单的爬虫的思路1_第3页
第3页 / 共10页
JAVA开发一个简单的爬虫的思路1_第4页
第4页 / 共10页
JAVA开发一个简单的爬虫的思路1_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《JAVA开发一个简单的爬虫的思路1》由会员分享,可在线阅读,更多相关《JAVA开发一个简单的爬虫的思路1(10页珍藏版)》请在金锄头文库上搜索。

1、JAVA开发一个简单的爬虫的思路1使用Get方法请求网页数据公司销售部门需求:需要将网站http:/ 的,这几天和一个网友聊天,他推荐给我一些也都蛮不错的。开始,准备工具:Eclipse 3.4.2 (下载地址:http:/www.eclipse.org/downloads/)Jdk 1.6.7 (下载地址:http:/ 3.1 相关jar包 (下载地址:http:/hc.apache.org/downloads.cgi)在写本文之前,所有使用的版本均为次新版,保证稳定性能,对应的最新版分别为:Eclipse 3.5.1Jdk 1.7HttpClient 4.1解压eclipse3.4.2,安

2、装jdk1.6.7,配置eclipse,设置eclipse使用的java虚拟机。如图:在eclipse菜单Window-Perferences中的Installed JREs新建工程新建普通的java工程,一切默认新建libs目录,将相关的包,加入到编译环境第一个jar包,是一个复合的包,将mons的相关包打进去,暂时用到orgapachepoi中的相关,可以到本文后面提供的下载路径下载工程。用apache poi包代替即可。Jar包找不到,可以到这里下载http:/ path-add to build path,这样,程序可以识别到外部引用的包新建带Main()的类Ef360Main.jav

3、aimport 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 mons.httpclient.HttpClient;import mons.httpclient.HttpException;import mons.httpclient.methods.GetMethod;imp

4、ort mons.logging.Log;import mons.logging.LogFactory;public class Ef360Main private static final String LOGON_SITE = http:/;private static final int LOGON_PORT = 80;private Log log = LogFactory.getLog(this.getClass();/用来写excel中的头描述private static String desc=new String公司介绍,主营产品,公司名称,所在省市,公司地址,邮编,联系人,联

5、系电话,手机号码,联系传真,网址,公司网址;/用来写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.getHo

6、stConfiguration().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);cli

7、ent.executeMethod(get);String responseString = get.getResponseBodyAsString();get.releaseConnection();/ 处理登录后首页的内容Matcher m = Ppile(Detail/.*html).matcher(responseString);while (m.find() Matcher m2 = Ppile(0-9+).matcher(m.group();if (m2.find() String findCode=m2.group();if(duclipcateCode.get(findCode

8、)!=null) continue;duclipcateCode.put(findCode, 1);String detailUrl = http:/ findCode + .html;Map result=new HashMap();System.out.println(访问子项:+detailUrl);/公司介绍String companyInfo=;/主营产品String products=;/公司名称String companyName=;/所在省市String province=;/公司地址String address=;/邮编String zipCode=;/联系人String h

9、eaderMan=;/联系电话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();compa

10、nyInfo=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=processDeta

11、il(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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号