0

Web サイトをスクレイピングして、 QueryPathの練習をしようとしています。

これが私がこれまでに持っているもので、エラーが発生します:

空の値からデフォルト オブジェクトを作成する

コード:

// URL to scrape
$baseurl = 'http://some-site-with-a-table-of-items-that-contain-links.com';

// Get all rows from table
$rows = htmlqp($baseurl, '#items_table')->find('tr');

//initialize items array
$items = array();

// initilize counter
$i = 0;

// Iterate through rows of items
foreach($rows as $row) {

    // get the url for the item in this row
    $url = qp($row)->find('.link_txt a')->attr('href');

    // select all the info in the item detail box
    $item = htmlqp($url)->find('.item_detail_box');

    // assign the item attributes to an array
    $items[$i] = [

        // the qp item $row is from the info on the main table of items
        'img_thumb'     => qp($row)->find('.reflection')->attr('src'),
        'name'          => qp($row)->find('.link_txt a')->text(),
        'item_level'    => qp($row)->find('.col_center')->text(),
        'req_level'     => qp($row)->find('.col_right')->text(),
        'url'           => $url,

        // the qp item $item is from the actual item detail page
        //'img'         => qp($item)->find('.reflection')->attr('src'),
        //'is_unique'   => qp($item)->find('.unique')->text(),


    ];

    $i++;
}

$data = print_r($items, true);

return '<pre>' . $data . '</pre>';

imgまたはis_unique配列行のいずれかのコメントを外すと、エラーが発生します。

他のすべてが機能し、それらの行がコメントアウトされている場合に期待される出力が得られます。

4

1 に答える 1

0

この問題は、QueryPath がアンカー タグからテキストを取得しようとしているセレクターから何も取得していないために発生しました。

各テーブル行のリンク/アンカーからテキストを取得しようとしていました。

ただし、ループの最初の行はテーブル ヘッダーであり、リンクのある行ではありませんでした。

ループにチェックを追加すると、問題が修正されました。

$url_ext = qp($row)->find('.ic_link_txt a')->attr('href');

    if ( $url_ext != NULL && $url_ext != "" ) {

これは、QueryPath について十分に知らなかった私の愚かな間違いでした。

(githubの問題にも関連しています https://github.com/technosophos/querypath/issues/130 )

于 2014-01-20T18:41:30.457 に答える