Java爬虫内容为ASCII码_简易的java爬虫项目

package com.GetContent;

import com.Bean.Herbal;

import com.ParsingPage.ParsingPage;

import com.SqlUtil.HerbalDao;

import com.Utils.StrUtil;

import org.htmlcleaner.HtmlCleaner;

import org.htmlcleaner.TagNode;

import org.htmlcleaner.XPatherException;

import java.util.List;

import java.util.Map;

public class GetContent {

public Herbal getContent(String url){

//创建用于返回结果的对象 Herbal herbalTemp=new Herbal();

//调用ParsingPage类的getContent方法获取页面内容,并存放在页面中。 String content=new ParsingPage().getContent(url);

//======下面是提取具体数据的方法====== //为了规范数据格式可能会用到我自己编写的StrUtil类进行格式化。 HtmlCleaner cleaner=new HtmlCleaner();

TagNode node=cleaner.clean(content);

Object[] ns =node.getElementsByAttValue("class", "drug py", true, true);

System.out.println("================================================================================================================");

//拼音 String py=null;

if (ns.length>0){

py=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("拼音","").replaceAll("①","");

py=StrUtil.getAsciiToStr(py);

herbalTemp.setPY(py);

System.out.println("拼音:"+py);

}

//拼音首字母 String py_code= StrUtil.getBigStr(py);

herbalTemp.setPy_code(py_code);

System.out.println("拼音首字母:"+py_code);

//英文名 ns = node.getElementsByAttValue("class", "drug ywm", true, true);

String ename=null;

if (ns.length>0){

ename=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("英文名","").replaceAll("①","");

herbalTemp.setEname(ename);

System.out.println("英文名:"+ename);

}

//名称 ns = node.getElementsByName("h1",true);

String herbal=null;

if (ns.length>0){

herbal=((TagNode)ns[0]).getText().toString();

herbalTemp.setHerbal(herbal);

System.out.println("名称:"+herbal);

}

//别名 String alias=null;

ns = node.getElementsByAttValue("class", "drug bm", true, true);

if (ns.length>0){

alias=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("别名","").replaceAll("①","");

herbalTemp.setAlias(alias);

System.out.println("别名:"+alias);

}

//来源 String source=null;

ns = node.getElementsByAttValue("class", "drug ly", true, true);

if (ns.length>0){

source=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("来源","").replaceAll("①","");

herbalTemp.setSource(source);

System.out.println("来源:"+source);

}

//性状 String trait=null;

ns = node.getElementsByAttValue("class", "drug xz", true, true);

if (ns.length>0){

trait=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("性状","").replaceAll("①","");

herbalTemp.setTrait(trait);

System.out.println("性状:"+trait);

}

//性味 String xw=null;

ns = node.getElementsByAttValue("class", "drug xw", true, true);

if (ns.length>0){

xw=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("性味","").replaceAll("①","");

}

Map s=StrUtil.getNatureAndTaste(xw);

//性味 String nature=s.get("nature");

//药味 String taste=s.get("taste");

herbalTemp.setNature(nature);

herbalTemp.setTaste(taste);

System.out.println("药味:"+taste+"\n"+"药性:"+nature);

//归经 String meridians=null;

ns = node.getElementsByAttValue("class", "drug gj", true, true);

if (ns.length>0){

meridians=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("归经","").replaceAll("①","");

herbalTemp.setMeridians(meridians);

System.out.println("归经:"+meridians);

}

//功能主治 String function=null;

ns = node.getElementsByAttValue("class", "drug gnzz", true, true);

if (ns.length>0){

function=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("功能主治","").replaceAll("①","");

}

//功效 用于前面 String efficacy=null;

//主治 用于。。。。。 String indications=null;

List stringList=StrUtil.getEfficacyAndIndication(function);

if (stringList.size()==1){

//功效 用于前面 efficacy=stringList.get(0);

herbalTemp.setEfficacy(efficacy);

}else if (stringList.size()==2){

//功效 用于前面 efficacy=stringList.get(0);

//主治 用于。。。。。 indications=stringList.get(1);

herbalTemp.setEfficacy(efficacy);

herbalTemp.setIndications(indications);

}

System.out.println("功效:"+efficacy+"\n"+"主治:"+indications);

//用法用量 usagedosage String usagedosage=null;

ns = node.getElementsByAttValue("class", "drug yfyl", true, true);

if (ns.length>0){

usagedosage=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("用法用量","").replaceAll("①","");

herbalTemp.setUsagedosage(usagedosage);

System.out.println("用法用量:"+usagedosage);

}

//炮制方法 process String process=null;

ns = node.getElementsByAttValue("class", "drug pz", true, true);

if (ns.length>0){

process=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("炮制","").replaceAll("①","");

herbalTemp.setProcess(process);

System.out.println("炮制方法:"+process);

}

//鉴别方法 identify ns = node.getElementsByAttValue("class", "drug jb", true, true);

String identify=null;

if (ns.length>0){

identify=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("鉴别","").replaceAll("①","");

herbalTemp.setIdentify(identify);

System.out.println("鉴别方法:"+identify);

}

//贮藏 ns = node.getElementsByAttValue("class", "drug zc", true, true);

String store=null;

if (ns.length>0){

store=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("贮藏","").replaceAll("①","");

herbalTemp.setStore(store);

System.out.println("贮藏:"+store);

}

//mattersneedattention 注意事项 ns = node.getElementsByAttValue("class", "drug jj", true, true);

String mattersneedattention=null;

if (ns.length>0){

mattersneedattention=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("禁忌","").replaceAll("①","");

herbalTemp.setMattersneedattention(mattersneedattention);

System.out.println("注意事项:"+mattersneedattention);

}

//contraindications 禁忌 ns = node.getElementsByAttValue("class", "drug zy", true, true);

String contraindications=null;

if (ns.length>0){

contraindications=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("注意","").replaceAll("①","");

herbalTemp.setContraindications(contraindications);

System.out.println("禁忌:"+contraindications);

}

//remark 备注 ns = node.getElementsByAttValue("class", "drug bz", true, true);

String remark=null;

if (ns.length>0){

remark=((TagNode)ns[0]).getText().toString().replaceAll(""","").replaceAll("备注","").replaceAll("①","");

herbalTemp.setRemark(remark);

System.out.println("备注:"+remark);

}

return herbalTemp;

}

// public static void main(String[] args) {// new GetContent().getContent("http://www.zysj.com.cn/zhongyaocai/yaocai_b/banlangen.html");// }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值