私はここ数日でこれをかなり調査しました。さまざまな機能についてオンラインですべての回答を見つけました。ありがとうございます。
私は今、ウェブページのコンテンツをすべて取得する 3 つの別々のコードを持っています (ページは、e コマースの製品ページ、レビュー ページ、製品が掲載されているものなど)、さまざまな情報を取得しますが、これは非常に中身を3回掴む非効率!
3 ビットのコードは、次の 3 つのことを行います: 1) Web ページのタイトルを取得する 2) ページからすべての画像を取得する 3) そのページのアイテムの価格を取得するための数値を検索する (できれば)。
ファイルの内容を一度だけ取得すればよいように、これらをグループ化するための助けをいただければ幸いです。これは私の現在のコードです:
function getDetails($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
//preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
//The above didnt work well enough (for getting Title when <title id=... > etc) so used the DOM below
preg_match("/(\£[0-9]+(\.[0-9]{2})?)/",$str,$price); //£ for GBP
$priceRes = preg_replace("/[^0-9,.]/", "", $price[0]);
//$pageDeatil[0]=$title;
$pageDeatil[1]=$priceRes;
return $pageDeatil;
}
}
$pageDeatil = getDetails("$newItem_URL");
//$itemTitle = $pageDeatil[0];
$itemPrice = $pageDeatil[1];
2回目:
$doc = new DOMDocument();
@$doc->loadHTMLFile("$newItem_URL");
$xpath = new DOMXPath($doc);
$itemTitle = $xpath->query('//title')->item(0)->nodeValue."\n";
3回目:
include('../../code/simplehtmldom/simple_html_dom.php');
include('../../code/url_to_absolute/url_to_absolute.php');
$html = file_get_html($newItem_URL);
foreach($html->find('img') as $e){
$imgURL = url_to_absolute($url, $e->src);
//More code here
}
ファイルを一度取得することはできないようで、残りはそれだけを使用します。どんな助けでも大歓迎です!前もって感謝します。