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
配列行のいずれかのコメントを外すと、エラーが発生します。
他のすべてが機能し、それらの行がコメントアウトされている場合に期待される出力が得られます。