単純なDOMセレクターにどのようにアクセスしますか?
質問
「クラス」アイテムのいくつかにアクセスできます
$ret = $html->find('articleINfo'); and then print the first key of the returned array.
ただし、span = id "firstarticle_0"のように必要な他のタグがありますが、それを見つけることができないようです。
$ret = $html->find('#span=id[ etc ]');
場合によっては何かが返されますが、それは配列ではなく、空のキーを持つ配列です。
残念ながら、var_dumpは1000ページの読み取り不可能なジャンクを生成するため、var_dumpを使用してオブジェクトを表示することはできません。コードは次のようになります。
<div id="articlething">
<p class="byline">By Lord Byron and <a href="www.marriedtothesea.com">Alister Crowley</a></p>
<p>
<span class="location">GEORGIA MOUNTAINS, Canada</span> |
<span class="timestamp">Fri Apr 29, 2011 11:27am EDT</span>
</p>
</div>
<span id="midPart_0"></span><span class="mainParagraph"><p><span class="midLocation">TUSCALOOSA, Alabama</span> - Who invented cheese? Everyone wants to know. They held a big meeting. Tom Cruise is a scientologist. </p>
</span><span id="midPart_1"></span><p>The president and his family visited Chuck-e-cheese in the morning </p><span id="midPart_2"></span><p>In Russia, 900 people were lost in the balls.</p><span id="midPart_3">
解決
単純なHTML DOMを簡単に使用して、特定のクラスのスパンを見つけることができます。
class = locationですべてのスパンが必要な場合は:
// create HTML DOM
$html = file_get_html($iUrl);
// get text elements
$aObj = $html->find('span[class=location]');
次に、次のようなことをします
foreach($aObj as $key=>$oValue)
{
echo $key.": ".$oValue->plaintext."<br />";
}
あなたの例を使って私のために私のために私の出力は次のとおりでした:
label = span、class =場所:見つかった1
0:カナダのジョージア山脈
それが役立つことを願っています...そして、シンプルなHTML DOMは、それが何をするかに最適であり、それを手に入れると使いやすいです。試してみると、何度も何度も使用する例がいくつかあります。私はかなりクレイジーなページを削りました、そして、それらはより簡単で簡単になります。
他のヒント
これを使用してみてください。私のために非常によく働いていて、非常に使いやすい。 http://code.google.com/p/phpquery/
PHP Simple Dom Parserのドキュメントは、オープングラフメタタグを解読することでむらがあります。これが私のために働いていると思われるものです:
<?php
// grab the contents of the page
$summary = file_get_html($url);
// Get image possibilities (for example)
$img = array();
// First, if the webpage has an og:image meta tag, it's easy:
if ($summary->find('meta[property=og:image]')) {
foreach ($summary->find('meta[property=og:image]') as $e) {
$img[] = $e->attr['content'];
}
}
?>
所属していません StackOverflow