代码之家  ›  专栏  ›  技术社区  ›  JVG

PHP-在scraper中使用变量和对象

  •  0
  • JVG  · 技术社区  · 12 年前

    我是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 ). 你知道我哪里错了吗?

    编辑: 作为参考,每一页上有四张图片 .extra_images ,所以我想将每个属性存储为一个单独的属性。

    1 回复  |  直到 12 年前
        1
  •  2
  •   Nick Pickering    12 年前

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

    $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);
    }
    

    没有一个可怕的混乱。