0

Simple PHP DOM Parser に問題があります。基本的に、画像とそのタイトルのカタログ サイトをスクレイピングする必要があります。

スクレイピングする必要があるサイトはhttp://pinesite.comです。

私はそれを行うために次のコードを思いつきました(これはAJAX経由で呼び出されます):

<?php
include ('simple_html_dom.php');
$function = $_GET['function'];
switch($function) {
  case 'subcat':
    $maincat = $_GET['cat'];
    $url = "http://www.pinesite.com/meubelen/index.php?".$maincat."&lang=de";
    $html = file_get_html($url);
    $data = $html->find('.box_166_content .act_path li a');
    $output ="";
    foreach ($data as $subcat) {
      $title = $subcat->plaintext;
      $href = $subcat->href;
      $link['title'] = $title;
      $link['href'] =substr($href,10);
      $output[] = $link;
    }
    echo json_encode($output);
    $html->clear();
    unset($html);
    unset($url);
    break;

  case 'images':
    $subcat = $_GET['subcat'];
    $url = "http://www.pinesite.com/meubelen/index.php?".$subcat;
    $html = file_get_html($url);
    $iframe = $html->find('#the_iframe',0);
    $url2 = $iframe->src;
    $html->clear(); 
    unset($html);

    $html2 = file_get_html("http://www.pinesite.com/meubelen/".$url2);
    $titles = $html2->find('p');
    $images = $html2->find('img');
    $output='';
    $i=0;
    foreach ($images as $image) {
      $item['title'] = $titles[$i]->plaintext;
      $item['thumb'] = $image->src;
      $item['image'] = str_replace('thumb_','',$image->src);
      $output[] = $item;
      $i++;
    }
    echo json_encode($output);
    break;
}
?>

それが「functions」ファイルで、動作しない部分は最後のケースです。

ここで何が問題なのかわからないので、別のファイルでテストしました(最後のケース)(iFrameから取得したURLを入れました(その部分は機能します):

<?php
include_once "simple_html_dom.php";

$fullurl = "http://www.pinesite.com/meubelen/prog/browse.php?taal=nl&groep=18&subgroep=26";

$html = file_get_html($fullurl);
$titles = $html->find('p');
$images = $html->find('img');
$output='';
$i=0;
foreach ($images as $image) {
  $item['title'] = $titles[$i]->plaintext;
  $item['thumb'] = $image->src;
  $item['image'] = str_replace('thumb_','',$image->src);
  $output[] =$item;
  $i++;
}
echo json_encode($output);
?>

私が言ったように、最初の部分は2番目と同じものを返す必要があります(?function=images&subcat=dichte-kastを追加した場合)が、そうではありません。パーサーを複数回使用しているためだと思います。

誰か私に提案はありますか?

4

2 に答える 2

1

問題は、$url2変数に html エンティティが含まれており、それをルート URL に連結すると、結果が有効な URL ではないという事実にあります。したがって、file_get_html()関数は期待する URL (およびデータ) を取得するのではなく、別のものを取得します。

問題の簡単な解決策はhtml_entity_decode()ですが、デバッグについても読みたいと思うかもしれません。使用しているすべての変数に適用するのと同じくらい簡単var_dump();で、出力が期待する出力と異なる場所を確認できます。

また、いくつかのセキュリティの問題についても確認することをお勧めします。書き込みは、直接$subcat = $_GET['sub_cat']使用するよりも決して安全ではありません。$_GET['sub_cat']

于 2011-11-15T15:39:54.250 に答える
0

質問を完全に理解しているかどうかはわかりませんが、収集できることから、特定のWebページからいくつかの画像とそれに関連するタイトルを取得して保存しようとしているということですか? その場合は、ここで検討の余地があります。(申し訳ありませんが、より具体的にはできませんでした)。

file_get_contents を使用して、html コンテンツを取得します。

$html = file_get_contents('www.someurl.com');

次に preg_match() で必要なすべてのイメージ タグとその他のデータを指定します。preg_matchを使用してIMGタグのSRC属性を一致させる方法については、多くの情報があります

 $matches = preg_match('<img>*<\/img>', $html); # this is a guess

画像タグのコレクションを配列として取得したら、curl を使用して画像を保存します

http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/

あなたが抱えている問題は、あなたが望むコンテンツからhtmlコンテンツを取り除くことだと思います

于 2011-11-15T15:44:38.380 に答える