예제 #1
0
파일: main.php 프로젝트: PLT875/WebHarvest
$linkXPath = 'id(\'productLister\')/ul/li/div/div/div/div/h3/a/@href';
$productLinks = WebScraper::retrieveNodeValuesFromDom($domDocument, $linkXPath);
// Retrieve an array of product unit prices (decimal format) that appear on the main URL.
$pricesXPath = '//p[contains(@class,"pricePerUnit")]';
$productPrices = WebScraper::retrieveNodeValuesFromDom($domDocument, $pricesXPath, '/[^0-9\\.]/');
// Retrieve the size of each product link found.
$linkSizes = array();
$productDescriptions = array();
foreach ($productLinks as $link) {
    $linkSource = WebScraper::retrievePageSourceSize($link);
    array_push($linkSizes, $linkSource['size']);
    // Retrieve an array of product descriptions that appear on the link.
    $domDocument = new DomDocument();
    @$domDocument->loadHTML($linkSource['source']);
    $descriptionXPath = 'id(\'information\')/productcontent/htmlcontent/div[1]/p[1]';
    $productDescription = WebScraper::retrieveNodeValuesFromDom($domDocument, $descriptionXPath);
    array_push($productDescriptions, $productDescription[0]);
}
// The product data attributes are all present on the main URL. So the productTitle
// key can be used across the other arrays to build a complete product record.
$results = array();
foreach ($productTitles as $key => $value) {
    $product = array('title' => $productTitles[$key], 'size' => $linkSizes[$key], 'unit_price' => $productPrices[$key], 'description' => $productDescriptions[$key]);
    array_push($results, $product);
}
// Calculate the total of all product unit prices.
$priceTotal = array_sum($productPrices);
$summary_keys = array('results', 'total');
$summary_values = array($results, $priceTotal);
$summary = array_combine($summary_keys, $summary_values);
$json_summary = json_encode($summary, JSON_PRETTY_PRINT);
 public static function BookRestaurant($username, $password, $url, $root, \model\Date $date)
 {
     $scrape = new WebScraper();
     $data = $scrape->get($url)->find('//form[@method="post"]')->getData();
     $scrape->reset();
     $post = $scrape->post(\URL::concatenate($root, $data[0]->getAttribute("action")), array('username='******'password='******'submit=login', 'group1=' . $date->dinner->value))->getData();
     if (isset($scrape->getInfo()['http_code']) && $scrape->getInfo()['http_code'] == 200) {
         return $post;
     } else {
         throw new \ScraperException("Failed getting a proper response from server when booking a table @\"{$url}\"");
     }
 }
예제 #3
0
 /**
  * Test that NULL is returned when searching for ID 'product' in $testPageSource.
  */
 public function testRetrieveElementByIdNoFind()
 {
     $testNoFind = WebScraper::retrieveElementByID($this->testPageSource, 'product');
     $this->assertEquals(is_null($testNoFind), TRUE);
 }