From 91137d2ac4cd1dcdc1b427082bb20ea8cf4edafb Mon Sep 17 00:00:00 2001 From: wrzzx <863076034@qq.com> Date: Wed, 15 Feb 2017 21:51:07 +0800 Subject: [PATCH] fix the extractor of the openhub_homepage(by liuyizhou) --- new_osseanextractor/.idea/encodings.xml | 3 +- new_osseanextractor/.idea/misc.xml | 3 + new_osseanextractor/.idea/vcs.xml | 2 +- new_osseanextractor/.idea/workspace.xml | 740 ++++++------------ new_osseanextractor/osseanextractor.iml | 2 +- .../main/java/core/ModelPageProcessor.java | 17 +- .../main/java/core/ModelPipeline4Update.java | 52 ++ .../src/main/java/core/Page.java | 4 +- .../main/java/core/PageModelExtractor.java | 30 +- .../java/core/PageModelPipeline4Update.java | 11 + .../src/main/java/extension/RawPage.java | 16 +- .../dao/OpenHubProject4Update_Dao.java | 19 + .../java/net/trustie/model/openhub_Model.java | 205 ++--- .../net/trustie/model/sourceforge_Model.java | 22 +- .../java/net/trustie/one/Application.java | 184 +---- .../java/net/trustie/one/ExtractThread.java | 206 +++++ .../main/java/net/trustie/one/Extractor.java | 4 + .../net/trustie/one/OpenhubReExtractor.java | 163 ++++ .../OpenHubProject_pipeline_4update.java | 34 + .../main/java/net/trustie/utils/Constant.java | 18 + .../utils/ExtractMutilLink4Openhub.java | 100 +++ project_match/bin/start_get_synonyms.sh~HEAD | 16 + .../bin/start_get_synonyms.sh~liuyizhou | 20 + 23 files changed, 1034 insertions(+), 837 deletions(-) create mode 100644 new_osseanextractor/src/main/java/core/ModelPipeline4Update.java create mode 100644 new_osseanextractor/src/main/java/core/PageModelPipeline4Update.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/dao/OpenHubProject4Update_Dao.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/one/ExtractThread.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/one/OpenhubReExtractor.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/pipeline/OpenHubProject_pipeline_4update.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/utils/Constant.java create mode 100644 new_osseanextractor/src/main/java/net/trustie/utils/ExtractMutilLink4Openhub.java create mode 100644 project_match/bin/start_get_synonyms.sh~HEAD create mode 100644 project_match/bin/start_get_synonyms.sh~liuyizhou diff --git a/new_osseanextractor/.idea/encodings.xml b/new_osseanextractor/.idea/encodings.xml index f20d44b48..b26911bd0 100644 --- a/new_osseanextractor/.idea/encodings.xml +++ b/new_osseanextractor/.idea/encodings.xml @@ -1,7 +1,6 @@ - + - \ No newline at end of file diff --git a/new_osseanextractor/.idea/misc.xml b/new_osseanextractor/.idea/misc.xml index a4c2ac661..a49fe5de4 100644 --- a/new_osseanextractor/.idea/misc.xml +++ b/new_osseanextractor/.idea/misc.xml @@ -1,5 +1,8 @@ + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -174,7 +141,6 @@ - @@ -201,6 +167,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -242,16 +287,14 @@ - - - + @@ -275,6 +318,26 @@ + + + + + + - - - - - - @@ -663,12 +704,14 @@ - + + - + + @@ -707,52 +750,49 @@ 1432444136539 - - - - - + + - - + - + - + - - - - + + + + - - + + - + - + - - - - + + + + - + - - + + @@ -771,378 +811,58 @@ - - - file://$PROJECT_DIR$/src/main/java/net/trustie/one/Application.java - 154 - - - - - - - - - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - + + - + + + + + + + + + + + + + + + + + + + + + @@ -1150,44 +870,64 @@ - + - - - - - - - - - - - + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + + diff --git a/new_osseanextractor/osseanextractor.iml b/new_osseanextractor/osseanextractor.iml index 67c50f7e4..dbab3e2c4 100644 --- a/new_osseanextractor/osseanextractor.iml +++ b/new_osseanextractor/osseanextractor.iml @@ -5,13 +5,13 @@ - + diff --git a/new_osseanextractor/src/main/java/core/ModelPageProcessor.java b/new_osseanextractor/src/main/java/core/ModelPageProcessor.java index 7b372e9ee..617adeed2 100644 --- a/new_osseanextractor/src/main/java/core/ModelPageProcessor.java +++ b/new_osseanextractor/src/main/java/core/ModelPageProcessor.java @@ -2,7 +2,13 @@ package core; import java.util.ArrayList; import java.util.List; - +/** + * + *

Title:站点页面模板生成进程

+ *

Description:生成站点页面模板

+ * @author liuyizhou + * @date 2016年12月23日 + */ public class ModelPageProcessor implements PageProcessor { private List pageModelExtractorList = new ArrayList(); @@ -14,6 +20,11 @@ public class ModelPageProcessor implements PageProcessor { return modelPageProcessor; } + /** + * 装载 站点页面模板生成器 列表 + * @param clazz 站点模板类对象 + * @return + */ public ModelPageProcessor addPageModel(Class clazz) { PageModelExtractor pageModelExtractor = PageModelExtractor .create(clazz); @@ -21,6 +32,10 @@ public class ModelPageProcessor implements PageProcessor { return this; } + /** + * + * @param page + */ public void process(Page page) { // TODO Auto-generated method stub for (PageModelExtractor pageModelExtractor : pageModelExtractorList) { diff --git a/new_osseanextractor/src/main/java/core/ModelPipeline4Update.java b/new_osseanextractor/src/main/java/core/ModelPipeline4Update.java new file mode 100644 index 000000000..79f5cd17f --- /dev/null +++ b/new_osseanextractor/src/main/java/core/ModelPipeline4Update.java @@ -0,0 +1,52 @@ +package core; + +import java.lang.annotation.Annotation; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import us.codecraft.webmagic.Task; +import us.codecraft.webmagic.model.annotation.ExtractBy; + +/** + * Created by LouAnt on 2017/2/14 0014 + * Description:更新Openhub社区Homepage专用 + */ +public class ModelPipeline4Update implements Pipeline{ + @SuppressWarnings("rawtypes") + private Map pageModelPipelines = new ConcurrentHashMap(); + + public ModelPipeline4Update() { + } + + public ModelPipeline4Update put(Class clazz, + PageModelPipeline4Update pageModelPipeline) { + pageModelPipelines.put(clazz, pageModelPipeline); + return this; + } + @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" }) + public void process(ResultItems resultItems,Task task){} + + public void processUpdate(ResultItems resultItems,String url, Task task){ + for(Map.EntryclassPageModelPipelineEntry : pageModelPipelines.entrySet()){ + boolean isSkip = resultItems.getFieldSkip(classPageModelPipelineEntry.getKey().getCanonicalName()); + if(isSkip) + continue; + + Object o = resultItems.get(classPageModelPipelineEntry.getKey().getCanonicalName()); + + if(o != null){ + Annotation annotation = classPageModelPipelineEntry.getKey().getAnnotation(ExtractBy.class); + + if(annotation == null || !((ExtractBy) annotation).multi()){ + classPageModelPipelineEntry.getValue().processUpdate(o,url,task); + }else{ + List list = (List)o; + for(Object o1 : list){ + classPageModelPipelineEntry.getValue().processUpdate(o1,url,task); + } + } + } + } + } +} diff --git a/new_osseanextractor/src/main/java/core/Page.java b/new_osseanextractor/src/main/java/core/Page.java index d49dd814e..dcd3ae496 100644 --- a/new_osseanextractor/src/main/java/core/Page.java +++ b/new_osseanextractor/src/main/java/core/Page.java @@ -8,9 +8,7 @@ import us.codecraft.webmagic.selector.Selectable; import us.codecraft.webmagic.utils.UrlUtils; /** - * - * @author Administrator - * + * 单页面实体 */ public class Page { private ResultItems resultItems = new ResultItems(); diff --git a/new_osseanextractor/src/main/java/core/PageModelExtractor.java b/new_osseanextractor/src/main/java/core/PageModelExtractor.java index 01ed7f929..ad8aef625 100644 --- a/new_osseanextractor/src/main/java/core/PageModelExtractor.java +++ b/new_osseanextractor/src/main/java/core/PageModelExtractor.java @@ -21,34 +21,59 @@ import us.codecraft.webmagic.selector.XpathSelector; import us.codecraft.webmagic.utils.ClassUtils; import us.codecraft.webmagic.utils.ExtractorUtils; +/** + * + *

Title:页面模板抽取器

+ *

Description:抽取页面模板

+ * @author liuyizhou + * @date 2016年12月23日 + */ public class PageModelExtractor { private Class clazz; private Extractor objectExtractor; private List fieldExtractors; private Logger logger = LoggerFactory.getLogger(getClass()); - + + /** + * 创建页面模板 + * @param clazz + * @return + */ public static PageModelExtractor create(Class clazz) { PageModelExtractor pageModelExtractor = new PageModelExtractor(); pageModelExtractor.init(clazz); return pageModelExtractor; } + //初始化页面模板 private void init(Class clazz) { this.clazz = clazz; + //初始化类对象抽取器objectExtractor initClassExtractors(); + //文件抽取器列表 fieldExtractors = new ArrayList(); + //遍历实体类的属性 for (Field field : ClassUtils.getFieldsIncludeSuperClass(clazz)) { + //设置实体类的属性可访问 field.setAccessible(true); + //获取实体类的对应属性的方法 FieldExtractor fieldExtractor = getAnnotationExtractBy(clazz, field); if (fieldExtractor != null) { + //检查页面模板文件格式 checkFormat(field, fieldExtractor); fieldExtractors.add(fieldExtractor); } } } + /** + * 检查页面模板格式 + * @param field + * @param fieldExtractor + */ private void checkFormat(Field field, FieldExtractor fieldExtractor) { // TODO Auto-generated method stub + //获取Field对象注解 Formatter formatter = field.getAnnotation(Formatter.class); if (formatter != null && !formatter.formatter().equals(ObjectFormatter.class)) { @@ -151,6 +176,9 @@ public class PageModelExtractor { } } + /** + *初始化类对象抽取器 + */ @SuppressWarnings("deprecation") private void initClassExtractors() { // TODO Auto-generated method stub diff --git a/new_osseanextractor/src/main/java/core/PageModelPipeline4Update.java b/new_osseanextractor/src/main/java/core/PageModelPipeline4Update.java new file mode 100644 index 000000000..f93ca365f --- /dev/null +++ b/new_osseanextractor/src/main/java/core/PageModelPipeline4Update.java @@ -0,0 +1,11 @@ +package core; + +import us.codecraft.webmagic.Task; + +/** + * Created by LouAnt on 2017/2/14 0014 + * Description: + */ +public interface PageModelPipeline4Update { + public void processUpdate(T t,String url, Task task); +} diff --git a/new_osseanextractor/src/main/java/extension/RawPage.java b/new_osseanextractor/src/main/java/extension/RawPage.java index 0fe7d0d02..a03cb6f5a 100644 --- a/new_osseanextractor/src/main/java/extension/RawPage.java +++ b/new_osseanextractor/src/main/java/extension/RawPage.java @@ -7,6 +7,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import core.Page; +/** + *

Title:站点详情实体类

+ *

Description:爬取到的站点页面HTML详情实体类

+ * @author liuyizhou + * @date 2016年12月23日 + */ public class RawPage { private int id; @@ -29,17 +35,15 @@ public class RawPage { this.page = new Page(); if (StringUtils.isNotBlank(this.url)) { if (this.html != null && this.html.length() > 0) { - page.setPageUrl(this.url); + page.setSkip(false); page.setRawText(this.html); - page.setSkip(false); - page.setTime(crawledTime); - } else { - page.setPageUrl(this.url); - page.setTime(crawledTime); + } else { page.setSkip(true); logger.warn("Warnning:" + this.toString() + " RawText Is Null OR Empty!"); } + page.setPageUrl(this.url); + page.setTime(crawledTime); } else { page.setSkip(true); logger.warn("Warnning:+" + this.toString() diff --git a/new_osseanextractor/src/main/java/net/trustie/dao/OpenHubProject4Update_Dao.java b/new_osseanextractor/src/main/java/net/trustie/dao/OpenHubProject4Update_Dao.java new file mode 100644 index 000000000..0f557a0b5 --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/dao/OpenHubProject4Update_Dao.java @@ -0,0 +1,19 @@ +package net.trustie.dao; + +import net.trustie.model.openhub_Model; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Update; + +/** + * Created by LouAnt on 2017/2/14 0014 + * Description: + */ +public interface OpenHubProject4Update_Dao { + + /** 根据url更新Homepage*/ + @Update("update openhub_project set `homepage`=#{homepage} where url=#{url} ") + public void updateHomepage(@Param("homepage") String homepage, + @Param("url") String url); +} diff --git a/new_osseanextractor/src/main/java/net/trustie/model/openhub_Model.java b/new_osseanextractor/src/main/java/net/trustie/model/openhub_Model.java index cf2561952..a6f896e2d 100644 --- a/new_osseanextractor/src/main/java/net/trustie/model/openhub_Model.java +++ b/new_osseanextractor/src/main/java/net/trustie/model/openhub_Model.java @@ -4,10 +4,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; -import java.util.HashSet; import java.util.List; import net.trustie.utils.DateHandler; +import net.trustie.utils.ExtractMutilLink4Openhub; import net.trustie.utils.Seperator; import net.trustie.utils.StringHandler; @@ -22,18 +22,6 @@ import core.AfterExtractor; import core.Page; import core.ValidateExtractor; import us.codecraft.webmagic.model.annotation.ExtractBy; -import us.codecraft.xsoup.Xsoup; - -import java.util.regex.Pattern; -import java.util.regex.Matcher; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - - @ExtractBy("//div[@id='projects_show_page']") public class openhub_Model implements AfterExtractor, ValidateExtractor { @@ -49,11 +37,26 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { private static String lastCommitTimeHeader = "most recent commit"; ///////////////////////////// + //@ExtractBy("//*[@id='page_contents']/div[3]/div[2]/div/dl/dd[1]/a[1]/@href") -//@ExtractBy("//*[@id="page_contents"]//a[@itemprop="url"]/@href") - @ExtractBy("//div/regex('.*[^a-zA-Z]Homepage[^a-zA-Z].*')") - private String homepage = ""; +//@ExtractBy("//*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/dl/dd[1]/a[1]/@href") +//@ExtractBy("//*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/dl/dd[2]/a[1]/@href " +//有Organization的单链接 +// "| //*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/dl/dd[1]/a[1]/@href" ) //无Organization的单链接 +// @ExtractBy(value="//a/regex(\".*Homepage\",1)") +//@ExtractBy("//*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/dl/dd[2]/a[@itemprop='url']/@href " + +// "| //*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/dl/dd[1]/a[@itemprop='url']/@href " + +// "| //*[@id='projects_show_page']/div[2]/div[3]/div[2]/div/*/*/a/regex(\".*Homepage\",1)/@href" //jquery multi links +// +"| //*[@id='projects_show_page']/div[2]/div[4]/div[2]/div/*/*/a/regex(\".*Homepage\",1)/@href " +//) + @ExtractBy(value="//a/regex(\"\",1) " ) + /*+ + " | //a/regex(\"\",1)" + + " | //a/regex(\"\",1)") + */ + +private List homepages = new ArrayList(); + private static String homepage =""; /////////////////////////////////// @@ -157,6 +160,12 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { * just for test */ + private List getHomepages(){ + return homepages; + } + public void setHomepages(List homepages){ + this.homepages = homepages; + } private String ansTmp = null; public String getAnsTmp() { @@ -189,15 +198,8 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { private String urlMd5; private String source = "openhub"; private int rateNum; - public void afterProcess(Page page) { - //此时的homepage是包含主页链接的整个a标签。需要进行进一步的处理。 - homepage = cleanHomepage(homepage); - - System.out.println(homepage); - - this.urlMd5 = DigestUtils.md5Hex(page.getPageUrl()); SimpleDateFormat bartDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); @@ -424,10 +426,11 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { this.rateLevel = this.rateLevel.substring(0, this.rateLevel.indexOf("/")); if (StringHandler.isAtLeastOneBlank(this.name, this.activity - /*,this. - description /* - ,this.homepage + ,this.description + */ + /* + ,getHomepage() */ /* , * this.licenses @@ -776,137 +779,13 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { ///////////// public String getHomepage() { - - return homepage; - } - - /* - /抽取a标签的主页链接(即目标a标签下href后面跟的链接),返回的可能是homepage的真实链接或是一个相对链接(此时有多个homepage) - */ - public String getHref(String homepage) { - String link = ""; - String patternString = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))"; - Pattern pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(homepage); - - while (matcher.find()) { - link=matcher.group().replaceAll("href\\s*=\\s*(['|\"]*)", "").replaceAll("['|\"]", ""); -// link=link.replaceAll("href\\s*=\\s*(['|\"]*)", ""); -// link=link.replaceAll("['|\"]", ""); - } - - return link; - } - - /* - /判断link是正常的主页链接,还是需要继续处理的相对链接。若是正常目标链接则返回true,若是相对链接则返回false - */ - public Boolean matchLink(String link) { - String patternTrueString = "http.*"; -// String patternFalseString = "/p.*"; - Pattern patternTrue = Pattern.compile(patternTrueString); -// Pattern patternFalse = Pattern.compile(patternFalseString); - - Matcher trueMatcher = patternTrue.matcher(link); - if(trueMatcher.find()) { - return true; - } else { - return false; - } - - } - - /** - * 抓取某个网页的源代码 - * @param urlstr 要抓取网页的地址 - * @param charset 网页所使用的编码 如"utf-8","gbk" - * @return 网页源码 - * @throws IOException - */ - public String fetchHtml(String urlstr, String charset) { - String result = ""; - try { - URL url = new URL(urlstr); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - InputStream is = con.getInputStream(); - InputStreamReader isr = new InputStreamReader(is, charset); - int read; - while ((read = isr.read()) != -1) { - result += (char) read; - } - isr.close(); - } catch(IOException e) { - e.printStackTrace(); - } - return result; - } - - /* - /处理双主页情况:获取真实的主页地址,并去重 - */ - public String dealWithDoubleHomepage(String link) { - - String homePageLink = ""; - String homepageHtml = fetchHtml(link,"UTF-8"); - Document doc = Jsoup.parse(homepageHtml); - - List list = Xsoup - .compile("//*[@id=\"page_contents\"]/div/div/div[1]//a[@target=\"_blank\"]/@href") - .evaluate(doc) - .list(); - - //去重 - HashSet h = new HashSet(list); - list.clear(); - list.addAll(h); - - for(String each : list) { - homePageLink = (homePageLink+" "+each).trim(); - } - homePageLink = homePageLink.replace(" ",";"); - - return homePageLink; - } - - - /* - 处理带homepage链接的a标签使之得到目标url的总接口方法 - */ - public String cleanHomepage(String homepage) { - - if(homepage != "") { - String link = ""; - link = getHref(homepage); - if(matchLink(link)) { - link = link.trim(); - return link; - } else { - String head = "https://www.openhub.net"; - link = (head + link).replace(" ","").trim(); - link = dealWithDoubleHomepage(link); - return link; - } - } else { - return null; - } +// return homepage; + return getHomepages().isEmpty() ? "" : getValidHomepage(getHomepages().get(0)); } public void setHomepage(String homepage) { -// String link; -// if(homepage != null) { -// link = getHref(homepage); -// if(matchLink(link)) { -// homepage = link; -// } else { -// String head = "https://www.openhub.net"; -// link = (head + link).replace(" ",""); -// homepage = link; -// //此处需要后续处理:将此页面爬取下来,并进一步解析页面中的homepage地址(有多个,并且可能需要去重) -// } -// } - this.homepage = homepage; -// System.out.println(this.homepage); - +// this.homepage = homepage; + this.homepage = getHomepages().isEmpty() ? "" : getHomepages().get(0); /////////////////////// } @@ -1397,11 +1276,15 @@ public class openhub_Model implements AfterExtractor, ValidateExtractor { public void setHistory(int history) { this.history = history; } - // public List getLinkInfos() { - // return linkInfos; - // } - // - // public void setLinkInfos(List linkInfos) { - // this.linkInfos = linkInfos; - // } + + + public String getValidHomepage(String homePage){ + String result = homePage; + //目前的Homepage抽取规则,以“/”开头的如"/p/jQuery"均是多链接的情形 + if(!homePage.equals("") && homePage.startsWith("/")){ + //获取Homepage列表中的Homepage + result = new ExtractMutilLink4Openhub().extractLinks(homePage); + } + return result; + } } diff --git a/new_osseanextractor/src/main/java/net/trustie/model/sourceforge_Model.java b/new_osseanextractor/src/main/java/net/trustie/model/sourceforge_Model.java index b1ebdbedb..e740c11b8 100644 --- a/new_osseanextractor/src/main/java/net/trustie/model/sourceforge_Model.java +++ b/new_osseanextractor/src/main/java/net/trustie/model/sourceforge_Model.java @@ -20,13 +20,17 @@ import core.Page; import core.ValidateExtractor; import us.codecraft.webmagic.model.annotation.ExtractBy; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + @ExtractBy("//body[@id='pg_project']/div[@id='page-body']/article[@id='project']") // 限定抽取区域 public class sourceforge_Model implements AfterExtractor, ValidateExtractor{ /////////////// -@ExtractBy("//a[@id='homepage']/@href") +@ExtractBy("//a[@id='homepage']/@href " + + " | //*[@class='homepage-link']/a/@href") private String homepage; ////////////////// @@ -609,4 +613,20 @@ public void setHomepage(String homepage) { + ", pageMD5=" + pageMd5 + ", history=" + history + "]"; } + /** + * 正则验证homepage的有效性 + * @param homepage + * @return + */ + private String checkHomepage(String homepage){ + String newHomepage = homepage; + String key = "^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"; + Pattern pattern = Pattern.compile(key,Pattern.CASE_INSENSITIVE); + //Homepage无效则使用页面url + if(null == homepage || "".equals(homepage) || !pattern.matcher(newHomepage).matches()){ + newHomepage = this.getUrl(); + } + return newHomepage; + } + } diff --git a/new_osseanextractor/src/main/java/net/trustie/one/Application.java b/new_osseanextractor/src/main/java/net/trustie/one/Application.java index 95968e5fe..18e5487d6 100644 --- a/new_osseanextractor/src/main/java/net/trustie/one/Application.java +++ b/new_osseanextractor/src/main/java/net/trustie/one/Application.java @@ -18,6 +18,7 @@ import net.trustie.dao.RecordDao; import net.trustie.downloader.DataBasePageErrorOutPut; import net.trustie.downloader.PageDao; import net.trustie.model.cto51_blog_Model; +import net.trustie.utils.Constant; import net.trustie.utils.DateHandler; import org.springframework.beans.BeansException; @@ -30,13 +31,22 @@ import core.ModelPipeline; import core.PageModelPipeline; import extension.RawPage; +/** + *

Title:抽取服务程序入口

+ *

Description:利用抽取模板,从爬虫数据库中抽取站点的项目、文档信息, + * 抽取至抽取数据库中对应站点的分表中

+ * @author + * @date 2016年12月22日 + */ @Component("Application") public class Application { - - private String sitesPath = "tasks.txt"; - //indicate if a site is being extracted + /** 站点列表配置文件路径 */ + private String sitesPath = Constant.SITE_PATH; + /** 抽取状态:indicate if a site is being extracted ,状态为true时表示正在抽取中无需重复抽取*/ public static Map extractState = new HashMap(); - private ExecutorService pool = Executors.newFixedThreadPool(20); + /** 线程池,用以装载抽取线程 参数:20为最大线程数 */ + private ExecutorService pool = Executors.newFixedThreadPool(Constant.POOL_MAX_SIZE); + public static void main(String args[]){ ((Application)AppContext.appContext.getBean("Application")).startJob(); } @@ -44,23 +54,23 @@ public class Application { public void startJob() { while(true){ //从文件中读取要抽取哪些站点 - List sites = readSites(); + List sites = readSitesFromConfig(); if(sites.size() == 0) continue; - - //看site被抽取过没,state为true说明已经抽取过了 + for( String site : sites){ Boolean state = extractState.get(site); if( state == null || state == false){ extractState.put(site, true); } - else if(state == true){ + else if(state == true){//抽取中,无需重复抽取 continue; } //用线程池抽取各个站点 ExtractThread et = (ExtractThread)AppContext.appContext.getBean("extractThread"); et.setSite(site); - pool.execute(et); + //提交任务到线程池,无返回值 + pool.execute(et); } try { System.out.println(Thread.currentThread().getName() + ": " + @@ -76,7 +86,11 @@ public class Application { } - private List readSites() { + /** + * 读取站点配置文件 + * @return 站点列表 + */ + private List readSitesFromConfig() { File file = new File(sitesPath); LinkedList tasks = new LinkedList(); BufferedReader reader = null; @@ -101,155 +115,5 @@ public class Application { } } -@Component("extractThread") -@Scope("prototype") -class ExtractThread implements Runnable{ - - private String site; - private static final int BatchExtractSize = 100; - public List modelName = new ArrayList(); - private Class pageModel; - @Resource - private PageDao pagedao; - @Qualifier("errorPageToDB") - @Autowired - private DataBasePageErrorOutPut pageErrorOutPut; - - @Resource - private RecordDao recordDao; - public ExtractThread(){ - - } - public ExtractThread(String site){ - this.site = site; - } - public void setSite(String site){ - this.site = site; - } - public void run() { - //设置threadname 以便查看输出日志 - Thread.currentThread().setName(site + "_thread"); - this.pageErrorOutPut.setTableName(site + "_error_page"); - try { - - System.out.println(Thread.currentThread().getName() + ": " + - "extract begain" + " @ " + DateHandler.getExtractTime()); - if(site.equals("51cto_blog")) - pageModel = cto51_blog_Model.class; - else - pageModel = Class.forName("net.trustie.model." + site +"_Model"); - modelName.add(pageModel.getCanonicalName()); - - //获取该站点的抽取模板和要抽取的html页面 - int lastId = getLastId(site); - List pages = getPages(site,lastId); - - Extractor extractor = new Extractor(); - RawPage result = null; - while(pages.size() > 0){ - - for( RawPage page : pages){ - try{ - result = extractor.extract(page,pageModel); - //持久化 并 更新抽取历史 - saveResult(site,result); - - - }catch (Exception e){ - e.printStackTrace(); - pageErrorOutPut.returnErrorPage(page, e); - } - } - updateLastId(site,lastId + pages.size()); - lastId = getLastId(site); - pages = getPages(site,lastId); - } - Application.extractState.put(site, false); - System.out.println(Thread.currentThread().getName() + ": " + - "extract over, id=" + lastId + " @ " + DateHandler.getExtractTime()); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch(Exception e){ - System.out.println(e.getMessage()); - e.printStackTrace(); - System.out.println(Thread.currentThread().getName() + ": " +" 发生异常 "); - } - - } - private void saveResult(String site, RawPage rawPage ) { - - ModelPipeline pl = new ModelPipeline(); - try { - pl.put(pageModel,(PageModelPipeline) AppContext.appContext.getBean(site + "_pipeline")); - } catch (BeansException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - if (!rawPage.getPage().getResultItems().isSkip()) { - pl.process(rawPage.getPage().getResultItems(), null); - } - if (!rawPage.getPage() - .isAllResultSkip( - modelName.toArray(new String[modelName - .size()]))) { - rawPage.setStored(true); - }else{ - - } - - if (!rawPage.isExtracted() || !rawPage.isStored()) - pageErrorOutPut.returnErrorPage(rawPage, - "May Caused By Model Problem! Or Page Error!"); - - - } - private void updateLastId(String site, int currentId) { - recordDao.updateRecord(site,currentId); - } - - private void saveResult(String site, List results) { - //System.out.println("save result"); - ModelPipeline pl = new ModelPipeline(); - try { - pl.put(pageModel,(PageModelPipeline) AppContext.appContext.getBean(site + "_pipeline")); - } catch (BeansException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - for(RawPage rawPage : results){ - if (!rawPage.getPage().getResultItems().isSkip()) { - pl.process(rawPage.getPage().getResultItems(), null); - } - if (!rawPage.getPage() - .isAllResultSkip( - modelName.toArray(new String[modelName - .size()]))) { - rawPage.setStored(true); - }else{ - System.out.println(rawPage.getPage().getResultItems()+"*****************************"); - } - - if (!rawPage.isExtracted() || !rawPage.isStored()) - pageErrorOutPut.returnErrorPage(rawPage, - "May Caused By Model Problem! Or Page Error!"); - } - } - - private List getPages(String site, int lastId) { - LinkedList pages = new LinkedList(); - pages = pagedao.getDetailPages(site + "_html_detail",lastId,BatchExtractSize); - return pages; - } - - private int getLastId(String site) { - Integer lastRecord = recordDao.getLastRecord(site); - if(lastRecord == null){ - lastRecord = pagedao.getMinId(site + "_html_detail") - 1; - recordDao.insertRecord(site,lastRecord); - } - return lastRecord; - } -} diff --git a/new_osseanextractor/src/main/java/net/trustie/one/ExtractThread.java b/new_osseanextractor/src/main/java/net/trustie/one/ExtractThread.java new file mode 100644 index 000000000..e85e8a24e --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/one/ExtractThread.java @@ -0,0 +1,206 @@ +package net.trustie.one; + +import core.ModelPipeline; +import core.PageModelPipeline; +import extension.RawPage; +import net.trustie.dao.RecordDao; +import net.trustie.downloader.DataBasePageErrorOutPut; +import net.trustie.downloader.PageDao; +import net.trustie.model.cto51_blog_Model; +import net.trustie.utils.Constant; +import net.trustie.utils.DateHandler; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + *

Title:抽取线程

+ *

Description:抽取项目的任务主线程

+ * @author liuyizhou + * @date 2016年12月22日 + */ +@Component("extractThread") +@Scope("prototype") +public class ExtractThread implements Runnable{ + /** 待抽取的站点名称 */ + private String site; + /** 待抽取结果条数限制 */ + private static final int BatchExtractSize = Constant.BATCH_EXTRACT_SIZE; + /** 模板名称列表 */ + public List modelName = new ArrayList(); + /** 动态生成站点模板 */ + private Class pageModel; + @Resource + private PageDao pagedao; + + /** 输出错误页面 */ + @Qualifier("errorPageToDB") + @Autowired + private DataBasePageErrorOutPut pageErrorOutPut; + + /** records表操作接口实例化 */ + @Resource + private RecordDao recordDao; + + public ExtractThread(){ + + } + + public ExtractThread(String site){ + setSite(site); + } + + public void setSite(String site){ + this.site = site; + } + + public void run() { + //设置thread name 以便查看输出日志 + Thread.currentThread().setName(site + "_thread"); + this.pageErrorOutPut.setTableName(site + "_error_page"); + try { + + System.out.println(Thread.currentThread().getName() + ": " + + "extract begain" + " @ " + DateHandler.getExtractTime()); + + if(site.equals("51cto_blog")) + pageModel = cto51_blog_Model.class; + else + pageModel = Class.forName("net.trustie.model." + site +"_Model"); + + modelName.add(pageModel.getCanonicalName()); + + //获取该站点的抽取模板和要抽取的html页面 + int lastId = getLastId(site); + List pages = getPages(site,lastId); + + Extractor extractor = new Extractor(); + RawPage result = null; + while(pages.size() > 0){ + + for( RawPage page : pages){ + try{ + result = extractor.extract(page,pageModel); + //持久化 并 更新抽取历史 + saveResult(site,result); + + + }catch (Exception e){ + e.printStackTrace(); + pageErrorOutPut.returnErrorPage(page, e); + } + } + updateLastId(site,lastId + pages.size()); + lastId = getLastId(site); + pages = getPages(site,lastId); + } + Application.extractState.put(site, false); + System.out.println(Thread.currentThread().getName() + ": " + + "extract over, id=" + lastId + " @ " + DateHandler.getExtractTime()); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch(Exception e){ + System.out.println(e.getMessage()); + e.printStackTrace(); + System.out.println(Thread.currentThread().getName() + ": " +" 发生异常 "); + } + + + } + private void saveResult(String site, RawPage rawPage ) { + + ModelPipeline pl = new ModelPipeline(); + try { + pl.put(pageModel,(PageModelPipeline) AppContext.appContext.getBean(site + "_pipeline")); + } catch (BeansException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if (!rawPage.getPage().getResultItems().isSkip()) { + pl.process(rawPage.getPage().getResultItems(), null); + } + if (!rawPage.getPage() + .isAllResultSkip( + modelName.toArray(new String[modelName + .size()]))) { + rawPage.setStored(true); + }else{ + + } + + if (!rawPage.isExtracted() || !rawPage.isStored()) + pageErrorOutPut.returnErrorPage(rawPage, + "May Caused By Model Problem! Or Page Error!"); + + + } + private void updateLastId(String site, int currentId) { + recordDao.updateRecord(site,currentId); + } + + private void saveResult(String site, List results) { + //System.out.println("save result"); + ModelPipeline pl = new ModelPipeline(); + try { + pl.put(pageModel,(PageModelPipeline) AppContext.appContext.getBean(site + "_pipeline")); + } catch (BeansException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + for(RawPage rawPage : results){ + if (!rawPage.getPage().getResultItems().isSkip()) { + pl.process(rawPage.getPage().getResultItems(), null); + } + if (!rawPage.getPage() + .isAllResultSkip( + modelName.toArray(new String[modelName + .size()]))) { + rawPage.setStored(true); + }else{ + System.out.println(rawPage.getPage().getResultItems()+"*****************************"); + } + + if (!rawPage.isExtracted() || !rawPage.isStored()) + pageErrorOutPut.returnErrorPage(rawPage, + "May Caused By Model Problem! Or Page Error!"); + } + } + + /** + * 获取站点site_html_detail表中,id大于lastId的 BatchExtractSize 条记录 + * @param site + * @param lastId + * @return + */ + private List getPages(String site, int lastId) { + LinkedList pages = new LinkedList(); + pages = pagedao.getDetailPages(site + "_html_detail",lastId,BatchExtractSize); + return pages; + } + + /** + * 获取站点site的最后id + * @param site + * @return + */ + private int getLastId(String site) { + /** 从records表中,获取最新的抽取记录的id */ + Integer lastRecord = recordDao.getLastRecord(site); + if(lastRecord == null){//最新抽取记录为空,则新增 + //从对应站点HTML详情表site_html_detail中,获取最新的id + lastRecord = pagedao.getMinId(site + "_html_detail") - 1;//TODO 为什么是min(ID) + //最新id插入records表 + recordDao.insertRecord(site,lastRecord); + } + return lastRecord; + } +} + diff --git a/new_osseanextractor/src/main/java/net/trustie/one/Extractor.java b/new_osseanextractor/src/main/java/net/trustie/one/Extractor.java index 044686907..b1c7c8cde 100644 --- a/new_osseanextractor/src/main/java/net/trustie/one/Extractor.java +++ b/new_osseanextractor/src/main/java/net/trustie/one/Extractor.java @@ -10,6 +10,10 @@ import core.ModelPageProcessor; import core.Pipeline; import extension.RawPage; +/** + * 抽取核心类 + * 主要执行 + */ public class Extractor { public List extract(List pages, Class modelClass ) { diff --git a/new_osseanextractor/src/main/java/net/trustie/one/OpenhubReExtractor.java b/new_osseanextractor/src/main/java/net/trustie/one/OpenhubReExtractor.java new file mode 100644 index 000000000..a88c412fe --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/one/OpenhubReExtractor.java @@ -0,0 +1,163 @@ +package net.trustie.one; + +import core.ModelPipeline; +import core.ModelPipeline4Update; +import core.PageModelPipeline; +import core.PageModelPipeline4Update; +import extension.RawPage; +import net.trustie.dao.RecordDao; +import net.trustie.downloader.DataBasePageErrorOutPut; +import net.trustie.downloader.PageDao; +import net.trustie.utils.Constant; +import net.trustie.utils.DateHandler; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Created by LouAnt on 2017/2/13 0013 + * Description:Openhub 社区项目Homepage字段重新抽取 + */ +@Component("ReExtractor") +public class OpenhubReExtractor { + /** 抽取状态:indicate if a site is being extracted ,状态为true时表示正在抽取中无需重复抽取*/ + public static Map extractState = new HashMap(); + /** 线程池,用以装载抽取线程 参数:20为最大线程数 */ + private ExecutorService pool = Executors.newFixedThreadPool(Constant.POOL_MAX_SIZE); + + public static void main(String[] args){ + ((OpenhubReExtractor) AppContext.appContext.getBean("ReExtractor")).start(); + } + + public void start(){ + while(true){ + ReExtractThread reExtractThread = (ReExtractThread)AppContext.appContext.getBean("reExtractThread"); + pool.execute(reExtractThread); + } + } +} + +/** + * 重新抽取的主线程 + */ +@Component("reExtractThread") +@Scope("prototype") +class ReExtractThread implements Runnable{ + /** 待抽取的站点名称 */ + private String site; + /** 待抽取结果条数限制 */ + private static final int BatchExtractSize = Constant.BATCH_EXTRACT_SIZE; + /** 模板名称列表 */ + public List modelName = new ArrayList(); + /** 动态生成站点模板 */ + private Class pageModel; + @Resource + private PageDao pagedao; + + /** 输出错误页面 */ + @Qualifier("errorPageToDB") + @Autowired + private DataBasePageErrorOutPut pageErrorOutPut; + + /** records表操作接口实例化 */ + @Resource + private RecordDao recordDao; + + public void run(){ + /**站点名称*/ + String site = "openhub"; + Thread.currentThread().setName(site + "reextractor_thread"); + this.pageErrorOutPut.setTableName(site+"_error_page"); + + try { + pageModel = Class.forName("net.trustie.model."+site+"_Model"); + + modelName.add(pageModel.getCanonicalName()); + + int lastId = getLastId(site); + + List pages = getPages(site,lastId); + + Extractor extractor = new Extractor(); + RawPage rawPage = null; + + while(pages.size() > 0){ + for(RawPage page : pages){ + try { + rawPage = extractor.extract(page, pageModel); + saveResult(site, rawPage); + }catch (Exception e){ + e.printStackTrace(); + // pageErrorOutPut.returnErrorPage(page, e);错误页面 + } + } + //更新抽取游标 + updateLastId(site,lastId + pages.size()); + lastId = getLastId(site); + pages = getPages(site,lastId); + } + Application.extractState.put(site, false); + System.out.println(Thread.currentThread().getName() + ": " + + "extract over, id=" + lastId + " @ " + DateHandler.getExtractTime()); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch(Exception e){ + System.out.println(e.getMessage()); + e.printStackTrace(); + System.out.println(Thread.currentThread().getName() + ": " +" 发生异常 "); + } + } + + private void saveResult(String site,RawPage rawPage){ + ModelPipeline4Update pl = new ModelPipeline4Update(); + try { + pl.put(pageModel, (PageModelPipeline4Update) AppContext.appContext.getBean(site + "_pipeline_4update")); + }catch (BeansException e){ + e.printStackTrace(); + } + + if(!rawPage.getPage().getResultItems().isSkip()){ + pl.processUpdate(rawPage.getPage().getResultItems(),rawPage.getUrl(),null); + } + } + + /** + * 获取站点site_html_detail表中,id大于lastId的 BatchExtractSize 条记录 + * @param site + * @param lastId + * @return + */ + private List getPages(String site, int lastId) { + LinkedList pages = new LinkedList(); + pages = pagedao.getDetailPages(site + "_html_detail",lastId,BatchExtractSize); + return pages; + } + + /** + * 获取站点site的最后id + * @param site + * @return + */ + private int getLastId(String site) { + /** 从records表中,获取最新的抽取记录的id */ + Integer lastRecord = recordDao.getLastRecord(site+"_reextractor"); + if(lastRecord == null){//最新抽取记录为空,则新增 + //从对应站点HTML详情表site_html_detail中,获取最新的id + lastRecord = pagedao.getMinId(site + "_html_detail") - 1; + //最新id插入records表 + recordDao.insertRecord(site+"_reextractor",lastRecord); + } + return lastRecord; + } + + private void updateLastId(String site, int currentId) { + recordDao.updateRecord(site+"_reextractor",currentId); + } +} diff --git a/new_osseanextractor/src/main/java/net/trustie/pipeline/OpenHubProject_pipeline_4update.java b/new_osseanextractor/src/main/java/net/trustie/pipeline/OpenHubProject_pipeline_4update.java new file mode 100644 index 000000000..3c030bf8c --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/pipeline/OpenHubProject_pipeline_4update.java @@ -0,0 +1,34 @@ +package net.trustie.pipeline; + +import core.PageModelPipeline4Update; +import net.trustie.dao.OpenHubProject4Update_Dao; +import net.trustie.model.openhub_Model; +import us.codecraft.webmagic.Task; + +import javax.annotation.Resource; + +import org.springframework.stereotype.Component; + +import net.trustie.dao.OpenHubProject_Dao; +import net.trustie.model.openhub_Model; +import core.PageModelPipeline; + +/** + * Created by LouAnt on 2017/2/14 0014 + * Description: + */ +@Component("openhub_pipeline_4update") +public class OpenHubProject_pipeline_4update implements PageModelPipeline4Update { + + @Resource + private OpenHubProject4Update_Dao openHubProject4Update_dao; + /** + * 更新Homepage专用 + * @param openhub_model + * @param task + */ + @Override + public void processUpdate(openhub_Model openhub_model, String url, Task task){ + openHubProject4Update_dao.updateHomepage(openhub_model.getHomepage(),url); + } +} diff --git a/new_osseanextractor/src/main/java/net/trustie/utils/Constant.java b/new_osseanextractor/src/main/java/net/trustie/utils/Constant.java new file mode 100644 index 000000000..474b06bb7 --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/utils/Constant.java @@ -0,0 +1,18 @@ +package net.trustie.utils; + +/** + *

Title:公共变量类

+ *

Description:用于存放全局静态变量

+ * @author liuyizhou + * @date 2016年12月22日 + */ +public class Constant { + /** 程序根目录*/ + private final static String CURRENT_PATH = System.getProperty("user.dir"); + /** 开源站点列表配置文件路径 */ + public final static String SITE_PATH = CURRENT_PATH + "\\tasks.txt"; + /** 线程池最大线程数 */ + public final static int POOL_MAX_SIZE = 20; + /** 抽取站点结果时,待抽取结果条数限制*/ + public final static int BATCH_EXTRACT_SIZE = 100; +} diff --git a/new_osseanextractor/src/main/java/net/trustie/utils/ExtractMutilLink4Openhub.java b/new_osseanextractor/src/main/java/net/trustie/utils/ExtractMutilLink4Openhub.java new file mode 100644 index 000000000..b4002d096 --- /dev/null +++ b/new_osseanextractor/src/main/java/net/trustie/utils/ExtractMutilLink4Openhub.java @@ -0,0 +1,100 @@ +package net.trustie.utils; + +import us.codecraft.webmagic.Page; +import us.codecraft.webmagic.Site; +import us.codecraft.webmagic.Spider; +import us.codecraft.webmagic.processor.PageProcessor; + +import java.util.List; + +/** + * Created by LouAnt on 2017/2/10 0010 + * Description:抽取openhub中多链接的项目 + */ +public class ExtractMutilLink4Openhub implements PageProcessor{ + private Site site = Site.me().setRetryTimes(5).setSleepTime(500); + + private static List homepages; + private static Page _page; + + static ExtractMutilLink4Openhub extractMutilLink4Openhub = new ExtractMutilLink4Openhub(); + + @Override + public void process(Page page) { + //抽取Homepage列表,Html格式参考 https://www.openhub.net/p/zimbra/links + ExtractMutilLink4Openhub.homepages = page.getHtml().xpath("//div[@class='links center-block']/div[1]/div[2]/div/h5/a").links().all(); + } + + @Override + public Site getSite() { + return site; + } + + public List getHomepages(String url){ + try{ + int count = 5; + //为防止一次抽取不成功,抽取5次 + while(ExtractMutilLink4Openhub.homepages==null && count > 0 ) { + ExtractMutilLink4Openhub.doExtract(url); + count--; + } + }catch(Exception e) { + + } + return ExtractMutilLink4Openhub.homepages; + } + + /** + * 获取多个link,用“;”间隔 + * @param url + * @return + */ + public String extractLinks(String url){ + String homepage = ""; + doExtract(url); + try { + List tempPages = getHomepages(url); + if (tempPages != null && !tempPages.isEmpty()) { + for (String link : tempPages) { + if(!homepage.contains(link)){//避免加入重复的链接 + homepage += link + ";"; + } + } + homepage = homepage.substring(0, homepage.lastIndexOf(";")); + } + }catch (Exception e){ + e.printStackTrace(); + } + return homepage; + } + + /** + * 获取第一个link作为Homepage + * @param url + * @return + */ + public String extractLink(String url){ + String homepage = url; + doExtract(url); + try { + List tempPages = getHomepages(url); + homepage = (tempPages != null && !tempPages.isEmpty()) ? tempPages.get(0) : url; + }catch (Exception e){ + e.printStackTrace(); + } + return homepage; + } + + /** + * 抽取Homepage核心方法 + * @param url 抽取目标url链接 + */ + public static void doExtract(String url){ + Spider.create(extractMutilLink4Openhub).addUrl(new String("https://www.openhub.net"+url)).thread(1).run(); + } + + public static void main(String[] args){ + String result = new ExtractMutilLink4Openhub().extractLink("/p/jQuery/links"); + System.out.println(result); + } +} diff --git a/project_match/bin/start_get_synonyms.sh~HEAD b/project_match/bin/start_get_synonyms.sh~HEAD new file mode 100644 index 000000000..4001d35ff --- /dev/null +++ b/project_match/bin/start_get_synonyms.sh~HEAD @@ -0,0 +1,16 @@ +#!/bin/bash + +find ./target/classes -name "*.properties"|xargs rm -f +find ./target/classes -name "*.xml"|xargs rm -f +find ./target/classes -name "*.dic"|xargs rm -f + +#export CLASSPATH=$CURR_DIR/lib:$CURR_DIR:$JAVA_HOME/lib:$JAVA_HOME/jre/lib + +tmp='./target/classes':$tmp +tmp='./target/Project_Match-0.0.1-SNAPSHOT-jar-with-dependencies-without-resources/*':$tmp +tmp='./bin/resources':$tmp +CLASSPATH=$tmp:$CLASSPATH + + +echo $CLASSPATH +java -classpath $CLASSPATH com.ossean.GetSynonyms >>log/getsynonyms_2017.log 2>&1 & diff --git a/project_match/bin/start_get_synonyms.sh~liuyizhou b/project_match/bin/start_get_synonyms.sh~liuyizhou new file mode 100644 index 000000000..c8e21dce8 --- /dev/null +++ b/project_match/bin/start_get_synonyms.sh~liuyizhou @@ -0,0 +1,20 @@ +#!/bin/bash + +find ./target/classes -name "*.properties"|xargs rm -f +find ./target/classes -name "*.xml"|xargs rm -f +find ./target/classes -name "*.dic"|xargs rm -f + +#export CLASSPATH=$CURR_DIR/lib:$CURR_DIR:$JAVA_HOME/lib:$JAVA_HOME/jre/lib + +tmp='./target/classes':$tmp +tmp='./target/Project_Match-0.0.1-SNAPSHOT-jar-with-dependencies-without-resources/*':$tmp +tmp='./bin/resources':$tmp +CLASSPATH=$tmp:$CLASSPATH + + +echo $CLASSPATH +JVM_ARGS="-Xmn98m -Xmx1024m -Xms512m -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxTenuringThreshold=2" +#echo JVM_ARGS=$JVM_ARGS +#ulimit -n 400000 +#echo "" > nohup.out +java $JVM_ARGS -classpath $CLASSPATH com.ossean.GetSynonyms >>log/getsynonyms_2017.log 2>&1 & \ No newline at end of file