屏幕抓取-PHP-在抓取器中使用变量和对象


screen scraping - PHP - Using variables and objects within a scraper

我是PHP的新手,已经尽了最大努力使用PHP参考指南,但我显然缺少一些东西。这是我的工作流程:

  1. 我有一个数组,里面存储了大约120个链接
  2. 我想抓取这些链接并从中获取产品信息
  3. 我想将该产品信息存储在数据库中

对于#3,我认为最好的方法是将信息存储在PHP对象中,然后将其导出到数据库中。如果我错了,请纠正我,还有更好的方法!

这是我的代码,当我试图分配属性时,它当前返回"PHP注意:试图在/home/scriptrunner/script.PHP中获取非对象的属性"错误(奇怪的是,只有当我获取属性$$productName->moreImages3 = $the_html->find(".extra_images ", 2)->src;时,这可能是转移注意力。

class Product{ //Creates an object class for products
    public $name = '';
    public $infoLink = '';
    public $description = '';
    public $mainImage = '';
    public $moreImages1 = '';
    public $moreImages2 = '';
    public $moreImages3 = '';
    public $moreImages4 = '';
    public $price = '';
    public $designer= '';
}

function getInfo($infoLink){    // Trawls the product pages for info  
    $the_content = scraperwiki::scrape($infoLink);
    $the_html = str_get_html($the_content);
    $productName = $the_html->find("#item_info h1", 0)->innertext;
    $$productName = new Product;
        $$productName->name = $productName;
        $$productName->infoLink = $infoLink;
        $$productName->designer = $the_html->find("#item_info h2", 0)->innertext;
        $$productName->description = $the_html->find("#item_info .product-body", 0)->innertext; //Might cause issues because there are multiple <p> tags in this div
        $$productName->mainImage = $the_html->find("#item_image .imagecache-product_item_default", 0)->src;
        $$productName->moreImages1 = $the_html->find(".extra_images ", 0)->src;
        $$productName->moreImages2 = $the_html->find(".extra_images ", 1)->src;
        $$productName->moreImages3 = $the_html->find(".extra_images ", 2)->src;
        $$productName->moreImages4 = $the_html->find(".extra_images ", 3)->src;
        $$productName->price = $the_html->find("#price", 0)->innertext;
        print_r($$productName ->name); //A test to see if it's working
}
for ($i = 0; $i<count($allLinks); ++$i){
   getInfo($allLinks[$i]);
};

for循环通过120个链路(包含在$allLinks中)。你知道我哪里错了吗?

EDIT:为了参考,每个页面上有四个.extra_images类的图像,所以我想将每个图像存储为一个单独的属性。

变量变量名从来都不是一个好主意,尤其是在批量中。只需使用一个数组:

$products[$productName] = new Product;
$products[$productName]->name = $productName;  
$products[$productName]->infoLink = $infoLink;
$products[$productName]->designer = $the_html->find("#item_info h2", 0)->innertext;
$products[$productName]->description = $the_html->find("#item_info .product-body", 0)->innertext; //Might cause issues because there are multiple <p> tags in this div
$products[$productName]->mainImage = $the_html->find("#item_image .imagecache-product_item_default", 0)->src;
$products[$productName]->moreImages1 = $the_html->find(".extra_images ", 0)->src;
$products[$productName]->moreImages2 = $the_html->find(".extra_images ", 1)->src;
$products[$productName]->moreImages3 = $the_html->find(".extra_images ", 2)->src;
$products[$productName]->moreImages4 = $the_html->find(".extra_images ", 3)->src;
$products[$productName]->price = $the_html->find("#price", 0)->innertext;

通过这种方式,您可以通过产品名称(如)轻松地单独访问产品

echo $products[$productName]->name;

如果需要,还可以循环浏览您的所有产品:

foreach($products as $product)
{
    var_dump($product);
}

没有一个可怕的混乱。