2

HTMLの解析に助けが必要です。ここに質問を投稿する前に、この回答を見つけようとしましたが、見つかりませんでした。ブログページの完全な html をデータベーステーブルに保存しました。次に、その html からテキストと画像を抽出します。しかし、HTML全体から段落固有のテキストと画像のみを抽出する必要があります。

以下の例を参照してください。多くのコードタグが含まれています。3 つの段落があります。私の要件に関連する段落2からのみテキストと画像を抽出する必要があります。(キーワードがあり、そのキーワードを検索して、この段落を抽出する必要があることを特定できます。)

ブログから特定の段落テキストと画像を抽出するにはどうすればよいですか。HTMLで検索するキーワードがあります。つまり、キーワード= PRODUCT ABCです。私はphpを使用しています。

<html>
<!-- Javascript: tag come here --->
<!-- Head: tag come here --->
<!-- Meta: tag come here --->
<!-- Title: tag come here --->
<!-- Links: tag come here --->
<!-- Javascript: tag come here --->

<body>

<!-- Lot of other code come here about links, javascript, headings etc -->
<!-- DIV: tag come here --->

<p> "PARAGRAPH 1, This paragraph contain only some text." </p>
<!-- Script: tag come here --->

<p> PARAGRAPH 2, It has some information about PRODUCT ABC...</p>
<img /> <!-- some images come here related to this paragraph.-->
<img /> <!-- some images come here related to this paragraph.-->
<img /> <!-- some images come here related to this paragraph.-->
<!-- Script: tag come here --->

<p> PARAGRAPH 3, This paragraph contain only some text. </p>
<img /> <!-- some images come here related to this paragraph.-->
<!-- Links: tag come here --->
<!-- Javascript: tag come here --->

</body>
</head>
</html>
4

2 に答える 2

0

tag簡単に抽出できるものを探している場合は、使用できますregex

簡単に:

$html = "<html><head></head><body><div>sometext</div><div><p>myPtag</p></div><div> some other text</div></body></html>";

preg_match('/<p>(.*?)<\/p>/',$html,$getTheP);

//and simply call what you want from extraction 
var_dump($getTheP);

それでも、タグで何かを一致させたい場合は<p>、新しいパスを簡単に作成して、必要なものを取得できます。

たとえば、 を<p>含む が必要ですsomestring

preg_match('/<p>(.*?)somestring<\/p>',$html,$matchesWithSomeString);

var_dump ( $matchesWithSomeString )

;

于 2014-05-27T13:52:49.503 に答える
0

私は夢想家に同意します。ただし、これは html フォーラムです。:P

次のコードを使用します。

$html = file_get_html(' http://www.google.com/ '); $par=$html->find('p[id=hello]') ; foreach($par->find('img') as $element) $element->src をエコーし​​ます。'
'

于 2013-05-26T23:46:52.200 に答える