式は、正の(b)は、達成プロセスによってWebページからタイトルやその他の情報を取得します

記事では、私が2012年に実現したコンテンツアグリゲーションサイトを紹介する表現は、Webコンテンツ内を描画されているとコードを提供します。

Webページから正規表現でのタイトル、URLを取得し、公開します

この記事では、そのさらなる実装プロセスをご紹介します。

(1)ページの構造解析 

  2012年ごろ、JavaScriptは、はるかに少ない強力で今日持っている骨格がhtmlページ、CSSの行動の装飾が施され、javascriptの提供のアクションだったです。

  [注]スタック型開発のフロントエンド、フロントエンドのコンテキストでやりたい今日、角度、Vueの技術で毎ターンで反応し、コンテンツの抽出はかなり異なる場合があります。

  Webページからプルコンテンツは、HTMLは、その構造を分析する必要があります。通常の状況下では、テーブル、UL、李、divタグとページのコンテンツは、一般的にdiv要素によって達成される、などでリストを提供することです。

  たとえば、ChromeのF12により、パーク「ダイジェスト」をブログ、

そして、リスト全体のページはdiv要素によって下支えされ、「要素を表示する」、それぞれがdivがあります。

前記ヘッダ及びリンクである<DIV CLASS =「post_item」> <クラス=「titlelnk」の下、

公開された、<ahref=和<span class="article_comment">間。

(2)コンテンツ抽出

        構造を知って、次のステップは、内容を抽出する方法です。

  • リスト全体のセクションを抽出

        この部分は比較的簡単ですが、それの端部のスタートリスト部分を見つけます。

        開始ブログパーク最良の部分は次のとおりです。<のdiv ID = "post_list"> 終了しました。</ div>

        一般情况下,开始位置比较容易确定,但结束位置这么些用正则表达式在结束标签是div的时候是无法获取内容的。这种情况下要找到结束标签的下一个标签,

        比如:

<script>editorPickStat(); aggSite.user.getUserInfo();</script>

 

1  // 获得网址所在的匹配区域部分
2 
3   private static String getContentArea(String urlContent, String strAreaBegin,String strAreaEnd) {
4 
5int pos1 = 0, pos2 = 0;
6 ​    pos1 = urlContent.indexOf(strAreaBegin) + strAreaBegin.length();
7 ​    pos2 = urlContent.indexOf(strAreaEnd, pos1);
8return urlContent.substring(pos1, pos2);
9   }

 

        然后通过正则表达式获取一个列表:

1 Pattern pt = Pattern.compile(rb.getRegex());
2 ​Matcher mt = pt.matcher(contentArea);
3 
4 while (mt.find()) {

 

  • 提取单条记录

    方法同提取内容部分差不多,如单条记录的起始标签为<div class="post_item">,结束标签相对来说也比较难确定。

    获取单条记录,并去除空格之类的无用字符:

    1 String rowContent = mt.group();
    2 
    3 rowContent = rowContent.replaceAll(rb.getRemoveRegex(), "");
  • 提取出标题

    取出标题用的是 >.?</a>或者title=.?>,并且要去除空格和多余的字符,需要用到类似:<li>|</li>|(<img)([\s\S]?)(>)|(<div)([\s\S]?)(>)|</div>或者(<table)([\s\S]?)(>)|<tr>|(<td)([\s\S]?)(>)|</td>|</tr>|(<table)([\s\S]*?)(>)之类的。

     1 // 获取标题
     2 
     3 Matcher title = Pattern.compile(rb.getTitleRegex()).matcher(rowContent);
     6 
     7 while (title.find()) {
     8 
     9 String s = title.group().replaceAll("||>|</a>|\[.*?\]|</l>","");
    10 
    11 if(s ==null || s.trim().length()<=0){
    13 s = "error";
    15 }
    17 tuBean.setTitle(s);
    18 
    19 }

     

  • 提取出超链接

    提取出超链接,需要用到 href=.?target=或者href=.?.> 或者 href=.*?title。要根据实际情况配置。

     1 // 获取网址
     2 
     3 Matcher myurl = Pattern.compile(rb.getUrlRegex()).matcher(
     4 
     5 rowContent);
     6 
     7 while (myurl.find()) {
     8 
     9 String u = myurl.group().replaceAll(
    10 
    11 "href=|"|>|target=|_blank|title", "");
    12 
    13 u = u.replaceAll("'|\\", "");
    14 
    15 if(u!=null && (u.indexOf("http://")==-1)){
    16 
    17 tuBean.setUrl(rb.getPrefix() + u);
    18 
    19 }else{
    20 
    21 tuBean.setUrl(u);
    22 
    23 }
    24 
    25 }
    26 
    27 if(tuBean.getUrl() ==null){
    28 
    29 tuBean.setUrl("error");
    30 
    31 }
    View Code

     

  • 提取出发表时间

不同的时间格式,获取方法是不一样的。这个也是用正则表达式获取:比较常规的

[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}

或者:

[0-9]{4}-[0-9]{2}-[0-9]{1,2}
1 // 获取时间
3 Matcher d = Pattern.compile(rb.getDateRegex()).matcher(rowContent);
5 while (d.find()) {
6 
7 tuBean.setDeliveryDate(d.group());
8 
9 }

おすすめ

転載: www.cnblogs.com/siweihz/p/12150017.html