1

初めて投稿します!

私が抱えている問題は、XPath と Tag-Soup を使用して Web ページを解析し、データを読み取っていることです。これらはニュース記事であるため、コンテンツにリンクが埋め込まれている場合があり、これらが私のプログラムを台無しにしています。

私が使用している XPath はstoryPath = "//html:article//html:p//text()";、ページが次の構造を持つ場所です。

<article ...>
   <p>Some text from the story.</p>
   <p>More of the story, which proves <a href="">what a great story this is</a>!</p>
   <p>More of the story without links!</p>
</article>

xpath 評価に関連する私のコードは次のとおりです。

NodeList nL = XPathAPI.selectNodeList(doc,storyPath);

LinkedList<String> story = new LinkedList<String>();
    for (int i=0; i<nL.getLength(); i++) {
        Node n = nL.item(i);

        String tmp = n.toString();
        tmp = tmp.replace("[#text:", "");
        tmp = tmp.replace("]", "");
        tmp = tmp.replaceAll("’", "'");
        tmp = tmp.replaceAll("‘", "'");
        tmp = tmp.replaceAll("–", "-");
        tmp = tmp.replaceAll("¬", "");
        tmp = tmp.trim();

        story.add(tmp);
    }

this.setStory(story);
...

private void setStory(LinkedList<String> story) {
    String tmp = "";
    for (String p : story) {
        tmp = tmp + p + "\n\n";
    }

    this.story = tmp.trim();
}

これが私に与える出力は

Some text from the story.

More of the story, which proves 

what a great story this is

!

More of the story without links!

このエラーを解消する方法はありますか? 私はどこかで間違ったアプローチを取っていますか?(setStory コードを使用できることは理解していますが、別の方法はありません。

tmp.replace() コードがなければ、すべての結果は [#text: what a great story this is] などのように表示されます。

編集:

おそらく別の種類ですが、私はまだ問題を抱えています..ここで私を殺しているのは再びリンクですが、BBCのウェブサイトの方法では、リンクは別の行にあるため、同じ問題でまだ読み込まれます前に説明したように (問題は与えられた例で修正されたことに注意してください)。BBC ページのコードのセクションは次のとおりです。

    <p>    Former Queens Park Rangers trainee Sterling, who 

    <a  href="http://news.bbc.co.uk/sport1/hi/football/teams/l/liverpool/8541174.stm" >moved to the Merseyside club in February 2010 aged 15,</a> 

    had not started a senior match for the Reds before this season.
    </p>

出力に次のように表示されます。

    Former Queens Park Rangers trainee Sterling, who 

    moved to the Merseyside club in February 2010 aged 15, 

         had not started a senior match for the Reds before this season.
4

3 に答える 3

1

重要[#text:なのは、単にtoString()DOMテキストノードの表現です。このtoString()メソッドは、デバッグ目的でノードの文字列表現が必要な場合に使用することを目的としています。実際のテキストを返すtoString()使用の代わりに。getTextContent()

リンクコンテンツを別々の行に表示したくない場合は//text()、XPathからを削除して、要素ノードのtextContentを直接取得できます(getTextContent()要素の場合、すべての子孫テキストノードの連結を返します)

String storyPath = "//html:article//html:p";
NodeList nL = XPathAPI.selectNodeList(doc,storyPath);

LinkedList<String> story = new LinkedList<String>();
for (int i=0; i<nL.getLength(); i++) {
    Node n = nL.item(i);
    story.add(n.getTextContent().trim());
}

手動で修正する必要があるという事実"‚Äô"は、HTMLが実際にはUTF-8でエンコードされていることを示唆していますが、Windows1252などのシングルバイト文字セットを使用して読み取っています。事後的に修正するのではなく、最初に正しいエンコーディングでデータを読み取る方法を検討する必要があります。

于 2013-03-05T12:24:42.950 に答える
1

HTMLソースコードの新しい行がテキストドキュメントに表示される編集の問題については、印刷する前にそれらを削除することをお勧めします。System.out.print(text.trim());する代わりにSystem.out.println(text.trim().replaceAll("[ \t\r\n]+", " "));

于 2013-03-05T15:07:18.350 に答える
1

最初に段落を見つけます: storyPath = "//html:article//html:p、次に各段落について、別の xpath クエリを使用してすべてのテキストを取得し、改行なしでそれらを連結し、段落の最後に 2 つの新しい行を配置します。

別の注意として、する必要はありませんreplaceAll("‚Äô", "'")。これは、ファイルを正しく開いていないことを示しています。ファイルを開くときは、リーダーをタグ スープに渡す必要があります。次のように Reader を初期化する必要Reader r = new BufferedReader(new InputStreamReader(new FileInputStream("myfilename.html"),"Cp1252"));があります。ファイルの正しい文字セットを指定する場所。文字セットのリストはこちら: http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html 私の推測では、Windows latin 1 です。

于 2013-03-05T12:03:43.727 に答える