找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3642|回复: 0

用爬虫建立全网最全疾病病症医学数据库

[复制链接]

307

主题

228

回帖

7349

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5599 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
发表于 2014-4-13 11:57:39 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
     做这个源于一个想法,比如你要查头痛,会列出所有可能导致头痛的疾病和该疾病其他症状,网上是不存在专业医学数据库这种东西的,因此需要自己从其他医学网站爬得,做到手机app中,并利用jsp网站进行大型数据库的查询是非常好的方法,输入一个词,可以搜索出医生/科室/位置/疾病/症状/相似信息。为了达到这个目的,必须有自己的医学数据库,而合作的医院并没提供,因此先自己实验一下了。现在的数据库已有180M,收录15000条内容

  1. package symptom;

  2. import java.io.IOException;
  3. import java.util.LinkedList;
  4. import java.util.Queue;

  5. import org.jsoup.Jsoup;
  6. import org.jsoup.nodes.Document;
  7. import org.jsoup.nodes.Element;
  8. import org.jsoup.select.Elements;

  9. import SQLite.Database;
  10. import SQLite.Exception;

  11. public class zzkfamilydoctorcomcn
  12. {
  13.         public final static int SYMPTOM=0;
  14.         public final static int DISEASE=1;
  15.         public static int threadnum=32;
  16.        
  17.         private Queue<ELE> dataQueue=new LinkedList<ELE>();//症状队列
  18.         public Database db=null;
  19.        
  20.         public class ELE
  21.         {
  22.                 public ELE(String url,int type)
  23.                 {
  24.                         this.url=url;
  25.                         this.type=type;
  26.                 }
  27.                
  28.                 /**数据库格式
  29.                  *         Name                症状/疾病名
  30.                         Id                        全局唯一标识
  31.                         IsSymptom        症状/疾病?
  32.                         BodyPart        身体部位                ----症状        头:眼睛
  33.                         Keshi                挂号科室                ----疾病        内科:男科
  34.                         Description        概述                       
  35.                         DDescription详述
  36.                         WithSymptom        伴随症状                ----疾病对应症状简述,逗号隔开
  37.                         WithDSymptom详细症状                ----疾病对应症状详述
  38.                         PossibleIll        相关疾病                ----症状可能疾病,大项逗号隔开,小项冒号隔开 疾病:科室:伴随症状
  39.                         Pathogeny        病因                       
  40.                         Check                检查
  41.                         Diagnosis        诊断鉴别
  42.                         Popularity        疾病/症状 搜索热度
  43.                  *
  44.                  */
  45.                
  46.                 public String getName()
  47.                 {
  48.                         return name;
  49.                 }

  50.                 public void setName(String name)
  51.                 {
  52.                         this.name = name;
  53.                 }

  54.                 public String getUrl()
  55.                 {
  56.                         return url;
  57.                 }

  58.                 public void setUrl(String url)
  59.                 {
  60.                         this.url = url;
  61.                 }

  62.                 public int getType()
  63.                 {
  64.                         return type;
  65.                 }

  66.                 public void setType(int type)
  67.                 {
  68.                         this.type = type;
  69.                 }

  70.                 public String getBodypart()
  71.                 {
  72.                         return bodypart;
  73.                 }

  74.                 public void setBodypart(String bodypart)
  75.                 {
  76.                         this.bodypart = bodypart;
  77.                 }

  78.                 public void setwithsymptom(String withsymptom)
  79.                 {
  80.                         this.withsymptom=withsymptom;
  81.                 }
  82.                
  83.                 public String getwithsymptom()
  84.                 {
  85.                         return this.withsymptom;
  86.                 }
  87.                
  88.                 private String name="";//症状名
  89.                 private String url="";//数据地址
  90.                 private int type=SYMPTOM;//疾病还是症状
  91.                 private String bodypart="";//身体部位        头:眼睛
  92.                 private String withsymptom="";
  93.                 private String keshi="";
  94.                
  95.                 public void resolve()
  96.                 {
  97.                         String description="";
  98.                         String ddescription="";
  99.                         String withdsymptom="";
  100.                         StringBuilder possibleill=new StringBuilder();
  101.                         String pathogeny="";
  102.                         String check="";
  103.                         String diagnosis="";
  104.                         threadnum--;
  105.                         String sql="";
  106.                        
  107.                         try
  108.                         {
  109.                                 Document doc=null;
  110.                                 try
  111.                                 {
  112.                                         doc = Jsoup.connect(url).timeout(0).get();
  113.                                 }
  114.                                 catch (java.lang.Exception e)
  115.                                 {
  116.                                         e.printStackTrace();
  117.                                         return;
  118.                                 }
  119.                                
  120.                                 if(type == SYMPTOM)
  121.                                 {//如果是症状
  122.                                         String[] dataset=doc.select("div.brumbs").text().split(" > ");
  123.                                         if(dataset.length <= 2)
  124.                                                 return;
  125.                                         name=dataset[dataset.length-1];
  126.                                         System.out.println(name);
  127.                                         bodypart=doc.select("div.jzxx a").get(2).text();
  128.                                         keshi=dataset[dataset.length-2];
  129.                        
  130.                                         description=doc.select("div.introductionTextBox").text();
  131.                                         ddescription=description;
  132.                                        
  133.                                         Elements tagnames=doc.select("div.subNav a");
  134.                                         for(Element tag:tagnames)
  135.                                         {
  136.                                                 if(tag.text().equals("病因"))
  137.                                                 {
  138.                                                         pathogeny=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.module.mPathogeny div.moduleContent").text();
  139.                                                 }
  140.                                                 else if(tag.text().equals("诊断"))
  141.                                                 {
  142.                                                         diagnosis=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.module.mFeature div.moduleContent").text();
  143.                                                 }
  144.                                         }

  145.                                         //处理相关疾病
  146.                                         Elements ll=doc.select("div.area.aXgjb div.colMain tr");
  147.                                         if(!ll.isEmpty())
  148.                                                 ll.remove(0);
  149.                                         if(!ll.isEmpty())
  150.                                         {
  151.                                                 for(Element ele:ll)
  152.                                                 {
  153.                                                         Elements temp2=ele.children();
  154.                                                         possibleill.append(temp2.get(0).text());
  155.                                                         possibleill.append(":");
  156.                                                         possibleill.append(temp2.get(1).text());
  157.                                                         possibleill.append(":");
  158.                                                         possibleill.append(temp2.get(2).text());
  159.                                                         possibleill.append(",");
  160.                                                 }
  161.                                                 if(possibleill.length() >= 1)
  162.                                                 {
  163.                                                         possibleill.deleteCharAt(possibleill.length()-1);//去掉多余的','
  164.                                                 }
  165.                                         }
  166.                                        
  167.                                         description=description.replace('\'',' ').replace('"',' ');
  168.                                         ddescription=ddescription.replace('\'',' ').replace('"',' ');
  169.                                         withsymptom=withsymptom.replace('\'',' ').replace('"',' ');
  170.                                         withdsymptom=withdsymptom.replace('\'',' ').replace('"',' ');
  171.                                         String ill=possibleill.toString().replace('\'',' ').replace('"',' ');
  172.                                         pathogeny=pathogeny.replace('\'',' ').replace('"',' ');
  173.                                         check=check.replace('\'',' ').replace('"',' ');
  174.                                         diagnosis=diagnosis.replace('\'',' ').replace('"',' ');
  175.                                        
  176.                                         sql="insert into symptomdata values('"+name+"','0','1','"+bodypart+"','"+keshi+"','"+description+
  177.                                                         "','"+ddescription+"','"+withsymptom+"','"+withdsymptom+"','"+ill+"','"+
  178.                                                         pathogeny+"','"+check+"','"+diagnosis+"','0')";
  179.                                         db.exec(sql,null);
  180.                                         System.out.println(name+" left:"+dataQueue.size());
  181.                                 }
  182.                                 else if(type == DISEASE)
  183.                                 {       
  184.                                         String[] dataset=doc.select("div.brumbs").text().split(" > ");
  185.                                         if(dataset.length <= 2)
  186.                                                 return;
  187.                                         name=dataset[dataset.length-1];
  188.                                         System.out.println(name);
  189.                                         bodypart=doc.select("span[itemprop=bodyLocation]").text();
  190.                                         keshi=dataset[dataset.length-2];
  191.                                        
  192.                                         description=doc.select("div.introductionTextBox").text();
  193.                                         ddescription=description;
  194.                                         Elements tagnames=doc.select("div.subNav a");
  195.                                        
  196.                                         for(Element tag:tagnames)
  197.                                         {
  198.                                                 if(tag.text().equals("病因"))
  199.                                                 {
  200.                                                         pathogeny=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.module.mPathogeny div.moduleContent").text();
  201.                                                 }
  202.                                                 else if(tag.text().equals("诊断"))
  203.                                                 {
  204.                                                         diagnosis=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.module.mFeature div.moduleContent").text();
  205.                                                 }
  206.                                                 else if(tag.text().equals("症状"))
  207.                                                 {
  208.                                                         withdsymptom=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.module.mIntro.mSymptom div.moduleContent").text();
  209.                                                 }
  210.                                         }
  211.                                                
  212.                                         //处理相关疾病
  213.                                         Elements ll=doc.select("div.area.aXgjb div.colMain tr");
  214.                                         if(!ll.isEmpty())
  215.                                                 ll.remove(0);
  216.                                         if(!ll.isEmpty())
  217.                                         {
  218.                                                 for(Element ele:ll)
  219.                                                 {
  220.                                                         Elements temp2=ele.children();
  221.                                                         possibleill.append(temp2.get(0).text());
  222.                                                         possibleill.append(":");
  223.                                                         possibleill.append(temp2.get(1).text());
  224.                                                         possibleill.append(":");
  225.                                                         possibleill.append(temp2.get(2).text());
  226.                                                         possibleill.append(",");
  227.                                                 }
  228.                                                 if(possibleill.length() >= 1)
  229.                                                 {
  230.                                                         possibleill.deleteCharAt(possibleill.length()-1);//去掉多余的','
  231.                                                 }
  232.                                         }
  233.                                        
  234.                                         description=description.replace('\'',' ').replace('"',' ');
  235.                                         ddescription=ddescription.replace('\'',' ').replace('"',' ');
  236.                                         withsymptom=withsymptom.replace('\'',' ').replace('"',' ');
  237.                                         withdsymptom=withdsymptom.replace('\'',' ').replace('"',' ');
  238.                                         String ill=possibleill.toString().replace('\'',' ').replace('"',' ');
  239.                                         pathogeny=pathogeny.replace('\'',' ').replace('"',' ');
  240.                                         check=check.replace('\'',' ').replace('"',' ');
  241.                                         diagnosis=diagnosis.replace('\'',' ').replace('"',' ');
  242.                                        
  243.                                         sql="insert into symptomdata values('"+name+"','0','0','"+bodypart+"','"+keshi+"','"+description+
  244.                                                         "','"+ddescription+"','"+withsymptom+"','"+withdsymptom+"','"+ill+"','"+
  245.                                                         pathogeny+"','"+check+"','"+diagnosis+"','0')";
  246.                                         db.exec(sql,null);
  247.                                         System.out.println(name+" left:"+dataQueue.size());
  248.                                 }
  249.                         }
  250.                         catch (IOException e)
  251.                         {
  252.                                 System.out.println(sql);
  253.                                 System.out.println(url);
  254.                                 e.printStackTrace();
  255.                         }
  256.                         catch (Exception e)
  257.                         {
  258.                                 System.out.println(sql);
  259.                                 System.out.println(url);
  260.                                 e.printStackTrace();
  261.                         }
  262.                         finally
  263.                         {
  264.                                 threadnum++;
  265.                         }
  266.                 }
  267.         }
  268.        
  269.         public zzkfamilydoctorcomcn()
  270.         {
  271.                 try
  272.                 {
  273.                         db=new Database();
  274.                         db.open("J:/symptom.db",0666);
  275.                        
  276. //                        //按症状查询
  277. //                        for(int i=1;i<8000;i++)
  278. //                        {
  279. //                                String cururl="http://zzk.familydoctor.com.cn/"+i;
  280. //                                dataQueue.offer(new ELE(cururl,SYMPTOM));
  281. //                        }
  282.                        
  283.                         //按疾病查询
  284.                         for(int i=6900;i<11000;i++)
  285.                         {
  286.                                 String cururl="http://jbk.familydoctor.com.cn/info"+i;
  287.                                 dataQueue.offer(new ELE(cururl,DISEASE));
  288.                         }
  289.                 }
  290.                 catch(Exception e)
  291.                 {
  292.                         e.printStackTrace();
  293.                 }
  294.                
  295.                 new Thread()
  296.                 {
  297.                         @Override
  298.                         public void run()
  299.                         {
  300.                                 try
  301.                                 {
  302.                                         sleep(1000);
  303.                                         while(!dataQueue.isEmpty())
  304.                                         {
  305.                                                 int num=32;
  306.                                                 while(num-- > 0 && threadnum > 0 && !dataQueue.isEmpty())
  307.                                                 {
  308.                                                         new Thread()
  309.                                                         {
  310.                                                                 @Override
  311.                                                                 public void run()
  312.                                                                 {
  313.                                                                         ELE curele=dataQueue.poll();
  314.                                                                         if(curele != null)
  315.                                                                                 curele.resolve();
  316.                                                                 }
  317.                                                         }.start();
  318.                                                 }
  319.                                                 sleep(200);
  320.                                         }
  321.                                 }
  322.                                 catch (InterruptedException e)
  323.                                 {
  324.                                         e.printStackTrace();
  325.                                 }
  326.                         }
  327.                 }.start();
  328.         }
  329.        
  330.         public static void main(String[] args)
  331.         {
  332.                 new zzkfamilydoctorcomcn();
  333.         }
  334. }

复制代码


  1. package symptom;

  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.LinkedList;
  5. import java.util.Queue;

  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.jsoup.nodes.Element;
  9. import org.jsoup.select.Elements;

  10. import SQLite.Database;
  11. import SQLite.Exception;

  12. public class tag120askcom
  13. {
  14.         public final static int SYMPTOM=0;
  15.         public final static int DISEASE=1;
  16.         public static int threadnum=10;
  17.         private String beginstr=null;
  18.        
  19.         private Queue<ELE> dataQueue=new LinkedList<ELE>();//症状队列
  20.         public Database db=null;
  21.         private int total;
  22.        
  23.         public class ELE
  24.         {
  25.                 public ELE(String name,String url,int type,String bodypart,String keshi)
  26.                 {
  27.                         if(name == null || name.equals(""))
  28.                                 System.out.println("error");
  29.                         this.name=name;
  30.                         this.url=url;
  31.                         this.type=type;
  32.                         this.keshi=keshi;
  33.                         this.bodypart=bodypart;
  34.                 }
  35.                
  36.                 /**数据库格式
  37.                  *         Name                症状/疾病名
  38.                         Id                        全局唯一标识
  39.                         IsSymptom        症状/疾病?
  40.                         BodyPart        身体部位                ----症状        头:眼睛
  41.                         Keshi                挂号科室                ----疾病        内科:男科
  42.                         Description        概述                       
  43.                         DDescription详述
  44.                         WithSymptom        伴随症状                ----疾病对应症状简述,逗号隔开
  45.                         WithDSymptom详细症状                ----疾病对应症状详述
  46.                         PossibleIll        相关疾病                ----症状可能疾病,大项逗号隔开,小项冒号隔开 疾病:科室:伴随症状
  47.                         Pathogeny        病因                       
  48.                         Check                检查
  49.                         Diagnosis        诊断鉴别
  50.                         Popularity        疾病/症状 搜索热度
  51.                  *
  52.                  */
  53.                
  54.                 public String getName()
  55.                 {
  56.                         return name;
  57.                 }

  58.                 public void setName(String name)
  59.                 {
  60.                         this.name = name;
  61.                 }

  62.                 public String getUrl()
  63.                 {
  64.                         return url;
  65.                 }

  66.                 public void setUrl(String url)
  67.                 {
  68.                         this.url = url;
  69.                 }

  70.                 public int getType()
  71.                 {
  72.                         return type;
  73.                 }

  74.                 public void setType(int type)
  75.                 {
  76.                         this.type = type;
  77.                 }

  78.                 public String getKeshi()
  79.                 {
  80.                         return keshi;
  81.                 }

  82.                 public void setKeshi(String keshi)
  83.                 {
  84.                         this.keshi = keshi;
  85.                 }

  86.                 public String getBodypart()
  87.                 {
  88.                         return bodypart;
  89.                 }

  90.                 public void setBodypart(String bodypart)
  91.                 {
  92.                         this.bodypart = bodypart;
  93.                 }

  94.                 private String name="";//症状名
  95.                 private String url="";//数据地址
  96.                 private int type=SYMPTOM;//疾病还是症状
  97.                 private String keshi="";//疾病科室(大科室)keshi+":"+血液内科
  98.                 private String bodypart="";//身体部位        头:眼睛
  99.                
  100.                 public void resolve()
  101.                 {
  102.                         String description="";
  103.                         String ddescription="";
  104.                         String withsymptom="";
  105.                         String withdsymptom="";
  106.                         StringBuilder possibleill=new StringBuilder();
  107.                         String pathogeny="";
  108.                         String check="";
  109.                         String diagnosis="";
  110.                         threadnum--;
  111.                         String sql="";
  112.                        
  113.                         try
  114.                         {
  115.                                 Document doc=Jsoup.connect(url).timeout(0).get();
  116.                                
  117.                                 if(type == SYMPTOM)
  118.                                 {//如果是症状
  119.                                         description=doc.select("dl.p_sibox1dl.clears p").text();
  120.                                         String temp=doc.select("dl.p_sibox1dl.clears p a").attr("abs:href");
  121.                                         ddescription=Jsoup.connect(temp).timeout(0).get().select("div.p_cleftartbox").text();
  122.                                        
  123.                                         Elements tagnames=doc.select("div.w_headnav_div a");
  124.                                         for(Element tag:tagnames)
  125.                                         {
  126.                                                 if(tag.text().equals("病因"))
  127.                                                 {
  128.                                                         pathogeny=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  129.                                                 }
  130.                                                 else if(tag.text().equals("检查"))
  131.                                                 {
  132.                                                         check=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  133.                                                 }
  134.                                                 else if(tag.text().equals("诊断鉴别"))
  135.                                                 {
  136.                                                         diagnosis=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  137.                                                 }
  138.                                                 else if(tag.text().equals("相关疾病"))
  139.                                                 {
  140.                                                         Elements ll=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("ul.p_cbox3ul");
  141.                                                         if(!ll.isEmpty())
  142.                                                         {
  143.                                                                 Elements eles=ll.get(0).children();
  144.                                                                 for(Element ele:eles)
  145.                                                                 {
  146.                                                                         possibleill.append(ele.select("span.p_cbox3li1 a").text());
  147.                                                                         possibleill.append(":");
  148.                                                                         possibleill.append(ele.select("span.p_cbox3li2 a").text());
  149.                                                                         possibleill.append(":");
  150.                                                                         possibleill.append(ele.select("span.p_cbox3li3 a").text());
  151.                                                                         possibleill.append(",");
  152.                                                                 }
  153.                                                                 if(possibleill.length() >= 1)
  154.                                                                 {
  155.                                                                         possibleill.deleteCharAt(possibleill.length()-1);//去掉多余的','
  156.                                                                 }
  157.                                                         }
  158.                                                 }
  159.                                         }
  160.                                        
  161.                                         description=description.replace('\'',' ').replace('"',' ');
  162.                                         ddescription=ddescription.replace('\'',' ').replace('"',' ');
  163.                                         withsymptom=withsymptom.replace('\'',' ').replace('"',' ');
  164.                                         withdsymptom=withdsymptom.replace('\'',' ').replace('"',' ');
  165.                                         String ill=possibleill.toString().replace('\'',' ').replace('"',' ');
  166.                                         pathogeny=pathogeny.replace('\'',' ').replace('"',' ');
  167.                                         check=check.replace('\'',' ').replace('"',' ');
  168.                                         diagnosis=diagnosis.replace('\'',' ').replace('"',' ');
  169.                                        
  170.                                         sql="insert into symptomdata values('"+name+"','0','1','"+bodypart+"','"+keshi+"','"+description+
  171.                                                         "','"+ddescription+"','"+withsymptom+"','"+withdsymptom+"','"+ill+"','"+
  172.                                                         pathogeny+"','"+check+"','"+diagnosis+"','0')";
  173.                                         db.exec(sql,null);
  174.                                         System.out.println(name+" left:"+dataQueue.size()+" total:"+total);
  175.                                 }
  176.                                 else if(type == DISEASE)
  177.                                 {       
  178.                                         description=doc.select("div.p_lbox1_ab p").text();
  179.                                         String temp=doc.select("div.p_lbox1_ab p a").attr("abs:href");
  180.                                         Document doc2=Jsoup.connect(temp).timeout(0).get();
  181.                                         ddescription=doc2.select("div.p_cleftartbox").text();
  182.                                        
  183.                                         Elements tagnames=doc.select("div.p_topbox a");
  184.                                         for(Element tag:tagnames)
  185.                                         {
  186.                                                 if(tag.text().equals("病因"))
  187.                                                 {
  188.                                                         pathogeny=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  189.                                                 }
  190.                                                 else if(tag.text().equals("检查"))
  191.                                                 {
  192.                                                         check=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  193.                                                 }
  194.                                                 else if(tag.text().equals("鉴别"))
  195.                                                 {
  196.                                                         diagnosis=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  197.                                                 }
  198.                                                 else if(tag.text().equals("症状"))
  199.                                                 {
  200.                                                         withdsymptom=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  201.                                                 }
  202.                                                 else if(tag.text().equals("并发症"))
  203.                                                 {
  204.                                                         withsymptom=Jsoup.connect(tag.attr("abs:href")).timeout(0).get().select("div.p_cleftartbox").text();
  205.                                                 }
  206.                                         }
  207.                                        
  208.                                         description=description.replace('\'',' ').replace('"',' ');
  209.                                         ddescription=ddescription.replace('\'',' ').replace('"',' ');
  210.                                         withsymptom=withsymptom.replace('\'',' ').replace('"',' ');
  211.                                         withdsymptom=withdsymptom.replace('\'',' ').replace('"',' ');
  212.                                         String ill=possibleill.toString().replace('\'',' ').replace('"',' ');
  213.                                         pathogeny=pathogeny.replace('\'',' ').replace('"',' ');
  214.                                         check=check.replace('\'',' ').replace('"',' ');
  215.                                         diagnosis=diagnosis.replace('\'',' ').replace('"',' ');
  216.                                        
  217.                                         sql="insert into symptomdata values('"+name+"','0','0','"+bodypart+"','"+keshi+"','"+description+
  218.                                                         "','"+ddescription+"','"+withsymptom+"','"+withdsymptom+"','"+ill+"','"+
  219.                                                         pathogeny+"','"+check+"','"+diagnosis+"','0')";
  220.                                         db.exec(sql,null);
  221.                                         System.out.println(name+" left:"+dataQueue.size()+" total:"+total);
  222.                                 }
  223.                         }
  224.                         catch (IOException e)
  225.                         {
  226.                                 System.out.println(sql);
  227.                                 System.out.println(url);
  228.                                 e.printStackTrace();
  229.                         }
  230.                         catch (Exception e)
  231.                         {
  232.                                 System.out.println(sql);
  233.                                 System.out.println(url);
  234.                                 e.printStackTrace();
  235.                         }
  236.                         finally
  237.                         {
  238.                                 threadnum++;
  239.                         }
  240.                 }
  241.         }
  242.        
  243.         public tag120askcom()
  244.         {
  245.                 try
  246.                 {
  247.                         db=new Database();
  248.                         db.open("J:/symptom.db",0666);

  249.                         Document doc=null;
  250.                         Elements eles=null;
  251.                        
  252.                         //按症状查询
  253.                         doc=Jsoup.connect("http://tag.120ask.com/zhengzhuang/").timeout(0).get();
  254.                         eles=doc.select("ul.p_leftdivnav").get(0).children();
  255.                         for(Element ele:eles)
  256.                         {
  257.                                 Elements test=ele.select("div.p_lnavdisdiv h3 a");
  258.                                 Document innerdoc=Jsoup.connect(test.attr("abs:href")).timeout(0).get();
  259.                                 Element toprocess=innerdoc.select("div.w_header").get(0);
  260.                                 String bigbodypart=toprocess.select("h3").text();
  261.                                 Elements subset=toprocess.select("a");
  262.                                
  263.                                 //先直接遍历症状
  264.                                 Elements innerele=innerdoc.select("div.w_neike.clears").get(0).children();
  265.                                 for(int i=0;i<innerele.size();i+=2)
  266.                                 {
  267.                                         String bigclass=innerele.get(i).text();//获取大科室名
  268.                                         for(Element tempele:innerele.get(i+1).select("a"))
  269.                                         {
  270.                                                 dataQueue.offer(new ELE(tempele.attr("title"),tempele.attr("abs:href"),SYMPTOM,bigbodypart,bigclass));
  271.                                         }
  272.                                 }
  273.                                 if(!subset.isEmpty())
  274.                                 {//有子节点则需要连接到每个网址然后遍历症状
  275.                                         for(Element curele:subset)
  276.                                         {
  277.                                                 Document temp=Jsoup.connect(curele.attr("abs:href")).timeout(0).get();
  278.                                                 Elements innerelel=temp.select("div.w_neike.clears").get(0).children();
  279.                                                 for(int i=0;i<innerelel.size();i+=2)
  280.                                                 {
  281.                                                         String bigclass=innerelel.get(i).text();//获取大科室名
  282.                                                         for(Element tempele:innerelel.get(i+1).select("a"))
  283.                                                         {
  284.                                                                 dataQueue.offer(new ELE(tempele.attr("title"),tempele.attr("abs:href"),SYMPTOM,
  285.                                                                                 bigbodypart+":"+curele.text(),bigclass));
  286.                                                         }
  287.                                                 }
  288.                                         }
  289.                                 }
  290.                         }
  291.                        
  292.                         //按疾病查询
  293.                         doc=Jsoup.connect("http://tag.120ask.com/jibing/").timeout(0).get();
  294.                         eles=doc.select("ul.p_leftdivnav").get(0).children();
  295.                         for(Element ele:eles)
  296.                         {
  297.                                 Elements test=ele.select("div.p_lnavdisdiv h3 a");
  298.                                 Document innerdoc=Jsoup.connect(test.attr("abs:href")).timeout(0).get();
  299.                                 Element toprocess=innerdoc.select("div.w_header").get(0);
  300.                                 String bigbodypart=toprocess.select("h3").text();
  301.                                 Elements subset=toprocess.select("a");
  302.                                 //先直接遍历症状
  303.                                 Elements innerele=innerdoc.select("div.w_neike.clears").get(0).children();
  304.                                 for(int i=0;i<innerele.size();i+=2)
  305.                                 {
  306.                                         String bigclass=innerele.get(i).text();//获取大科室名
  307.                                         for(Element tempele:innerele.get(i+1).select("a"))
  308.                                         {
  309.                                                 dataQueue.offer(new ELE(tempele.attr("title"),tempele.attr("abs:href"),DISEASE,bigbodypart,bigclass));
  310.                                         }
  311.                                 }
  312.                                 if(!subset.isEmpty())
  313.                                 {//有子节点则需要连接到每个网址然后遍历症状
  314.                                         for(Element curele:subset)
  315.                                         {
  316.                                                 Document temp=Jsoup.connect(curele.attr("abs:href")).timeout(0).get();
  317.                                                 Elements innerele1=innerdoc.select("div.w_neike.clears").get(0).children();
  318.                                                 for(int i=0;i<innerele1.size();i+=2)
  319.                                                 {
  320.                                                         String bigclass=innerele1.get(i).text();//获取大科室名
  321.                                                         for(Element tempele:innerele1.get(i+1).select("a"))
  322.                                                         {
  323.                                                                 dataQueue.offer(new ELE(tempele.attr("title"),tempele.attr("abs:href"),DISEASE,bigbodypart,bigclass));
  324.                                                         }
  325.                                                 }
  326.                                         }
  327.                                 }
  328.                         }
  329.                         System.out.println(dataQueue.size());
  330.                         for(ELE ele:dataQueue)
  331.                         {
  332.                                 System.out.println(ele.name);
  333.                         }
  334.                         total=dataQueue.size();
  335.                        
  336.                         if(beginstr != null)
  337.                         {
  338.                                 while(!dataQueue.poll().getName().equals(beginstr))
  339.                                 {
  340.                                         ;
  341.                                 }
  342.                         }
  343.                 }
  344.                 catch (FileNotFoundException e)
  345.                 {
  346.                         e.printStackTrace();
  347.                 }
  348.                 catch (IOException e)
  349.                 {
  350.                         e.printStackTrace();
  351.                 }
  352.                 catch (Exception e)
  353.                 {
  354.                         e.printStackTrace();
  355.                 }
  356.                
  357.                 new Thread()
  358.                 {
  359.                         @Override
  360.                         public void run()
  361.                         {
  362.                                 try
  363.                                 {
  364.                                         sleep(1000);
  365.                                         while(!dataQueue.isEmpty())
  366.                                         {
  367.                                                 int num=1;
  368.                                                 while(num-- > 0 && threadnum > 0 && !dataQueue.isEmpty())
  369.                                                 {
  370.                                                         new Thread()
  371.                                                         {
  372.                                                                 @Override
  373.                                                                 public void run()
  374.                                                                 {
  375.                                                                         ELE curele=dataQueue.poll();
  376.                                                                         if(curele != null)
  377.                                                                                 curele.resolve();
  378.                                                                 }
  379.                                                         }.start();
  380.                                                 }
  381.                                                 sleep(200);
  382.                                         }
  383.                                 }
  384.                                 catch (InterruptedException e)
  385.                                 {
  386.                                         e.printStackTrace();
  387.                                 }
  388.                         }
  389.                 }.start();
  390.         }
  391.        
  392.         public static void main(String[] args)
  393.         {
  394.                 new tag120askcom();
  395.         }
  396. }


复制代码




回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-5-6 12:00 , Processed in 0.049002 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表