皆さん、こんにちは。
結果をこすり取ろうとして成功しましたが、今は立ち往生しています。
以下のコードは、クラスが「vsc」の DIV があり、その中にクラスが「r」の H3 があることを示しています。(//h3[@class='r'//a) で H3 タグ内のアンカーを取得できます。
私の問題は、以下のテーブルにも「r」のクラスを持つ H3 があり、テーブル内のリンクが必要ないことです。
<li class="g">
<div class="vsc" pved="0CD4QkgowAA" bved="0CD8QkQo" sig="m15">
<h3 class="r">
<a href="https://ameriloan.com/" class="l" onmousedown="return rwt(this,'','','','1','AFQjCNEazKuyTuAyYgnAT3MqI3aJoiAlZw','','0CDwQFjAA',null,event)">
</h3>
<div class="vspib" aria-label="Result details" role="button" tabindex="0">
<div class="s">
</div>
<table cellpadding="0" cellspacing="0" class="nrgt">
すべてのアンカーをスクレイピングするために使用しているスクリプトを次に示しますが、「vsc」DIV の H3 アンカーのみを取得することはできません。
function getURL($url)
{
$ch=curl_init();
// This allows the script to accept HTTPS certificates "blindly"
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follows redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 6); // follows up to 6 redirects
$ret = curl_exec($ch);
return $ret;
}
$i = 0;
$rawKeyword = 'EXAMPLE';
$keyword = str_replace(' ', '+', $rawKeyword);
$url = "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=".$keyword;
//get the HTML through cURL function
$html = getURL($url);
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all data
$xpath = new DOMXPath($dom);
// XPath eval to get page links and titles
//$elementContent = $xpath->evaluate("//h3[@class='r']//a");
$elementContent = $xpath->evaluate("//div[@class='vsc']//h3[@class='r']//a");
// Print results
foreach ($elementContent as $content) {
$i++;
$clean = trim($content->getAttribute('href'), "/url?q=");
echo '<strong>'.$i.'</strong>: <h3 style=" clear:none !important; font-size:10px; letter-spacing:0.1em; line-height:2.6em; text-transform:uppercase;">'.$content->textContent.'</h3><br/>'.$clean.'<br /><br />';
}
評価クエリで何が間違っていますか?
@jdwilemo - 「vsc」のクラスでDIV内のアンカーのみを取得しようとしている方法であなたは正しいです。これは、「r」のクラスを持つ他の H3 DIV を示すテーブル コードの詳細です...
<table cellpadding="0" cellspacing="0" class="nrgt">
<tbody>
<tr class="mslg">
<td style="vertical-align: top; ">
<div class="sld vsc" pved="0CIYBEJIKMAE" bved="0CIcBEJEK" sig="Q_U">
<span class="tl">
<h3 class="r">
<a href="https://example.com/?page=ent_cs_login" class="l" onmousedown="return rwt(this,'','','','2','AFQjCNEyANjoolNXGFnLVKH3S1j4CO1qQw','','0CIQBEIwQMAE',null,event)">
</h3>
</span>
<div class="vspib" aria-label="Result details" role="button" tabindex="0">
<div class="s">
</div>
</li>
すべて「li」タグで囲みます。テーブルは「li」タグの最後の要素です。「li」要素の末尾にあるテーブル内の < H3 class='r'> アンカーを取得せずに < H3 class='r'> アンカーを取得したい。私はそれをクリアしたことを願っています...