爬虫学习之htmlparser分析网页

爬虫学习之htmlparser分析网页

ID:13796343

大小:29.50 KB

页数:3页

时间:2018-07-24

爬虫学习之htmlparser分析网页_第1页
爬虫学习之htmlparser分析网页_第2页
爬虫学习之htmlparser分析网页_第3页
资源描述:

《爬虫学习之htmlparser分析网页》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、在进行下述例子之前,请添加上面所用到的依赖包!1、通过HTMLParser将网页下载到本地StringlocalFile="F:\temp\temp.html";try{//HttpClient主要负责执行请求HttpClienthttpClient=newDefaultHttpClient();//利用HTTPGET向服务器发起请求HttpGetget=newHttpGet("http://www.mwcly.cn");//里面参数为要爬取的网址,假如你的网址是www.huaihua365.net那么这里就填写你的网址//获得服务器响应的所有消息HttpResponseresponse

2、=httpClient.execute(get);//获得服务器响应回来的消息体(不包括HTTPHEAD)HttpEntityentity=response.getEntity();if(entity!=null){//获取编码信息Stringcharset=EntityUtils.getContentCharSet(entity);InputStreamis=entity.getContent();IOUtils.copy(is,newFileOutputStream(localFile));}//获得所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpClient.getCo

3、nnectionManager().shutdown();}catch(Exceptione){}2、提取网页中图片的链接地try{//把文件的内容读进来Stringhtml=IOUtils.toString(newFileInputStream(localFile),"UTF-8");//创建一个HTML解释器Parserparser=newParser();parser.setInputHTML(html);//图片节点NodeListimageTags=parser.parse(newNodeClassFilter(ImageTag.class));System.out.println

4、(imageTags.size());for(inti=0,size=imageTags.size();i

5、Trace();}1、提取具有某种特征的标签try{//把文件的内容读进来Stringhtml=IOUtils.toString(newFileInputStream(localFile),"UTF-8");//创建一个HTML解释器Parserparser=newParser();parser.setInputHTML(html);//提取name="title"的meta标签NodeListmetaTags=parser.parse(newNodeFilter(){publicbooleanaccept(Nodenode){if(nodeinstanceofMetaTag){MetaTa

6、gmt=(MetaTag)node;if(mt.getMetaTagName()!=null&&mt.getMetaTagName().equals("description")){returntrue;}}returnfalse;}});System.out.println(metaTags.size());for(inti=0,size=metaTags.size();i

7、}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}catch(ParserExceptione){e.printStackTrace();}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。