2017-06-16 51 views
0

所以我想放棄這個頁面重定向到本身: http://www.asx.com.au/asx/statistics/todayAnns.do廢料HTML頁面,使用PHP捲曲

看來,我的代碼不能得到整個頁面的html代碼,它的行爲非常奇怪。

我試過簡單的HTML DOM,但沒有任何作品。

$base = "http://www.asx.com.au/asx/statistics/todayAnns.do"; 

    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_URL, $base); 
    curl_setopt($curl, CURLOPT_REFERER, $base); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
    $str = curl_exec($curl); 
    curl_close($curl); 
    echo htmlspecialchars($str); 

這顯示了主要是JavaScript和我無法獲得頁面。我的目標是在網址上取消該中間表。

回答

1

如果您不需要最新的數據,那麼您可以使用谷歌網頁的緩存版本。

<?php 

use Scraper\Scrape\Crawler\Types\GeneralCrawler; 
use Scraper\Scrape\Extractor\Types\MultipleRowExtractor; 

require_once(__DIR__ . '/../vendor/autoload.php'); 
date_default_timezone_set('UTC'); 

// Create crawler 
$crawler = new GeneralCrawler(
    'http://webcache.googleusercontent.com/search?q=cache:http://www.asx.com.au/asx/statistics/todayAnns.do&num=1&strip=0&vwsrc=0' 
); 

// Setup configuration 
$configuration = new \Scraper\Structure\Configuration(); 
$configuration->setTargetXPath('//div[@class="page"]//table'); 
$configuration->setRowXPath('.//tr'); 
$configuration->setFields(
    [ 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Headline', 
       'xpath' => './/td[3]', 
      ] 
     ), 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Published', 
       'xpath' => './/td[1]', 
      ] 
     ), 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Pages', 
       'xpath' => './/td[4]', 
      ] 
     ), 
     new \Scraper\Structure\AnchorField(
      [ 
       'name'    => 'Link', 
       'xpath'    => './/td[5]/a', 
       'convertRelativeUrl' => false, 
      ] 
     ), 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Code', 
       'xpath' => './/text()', 
      ] 
     ), 
    ] 
); 

// Extract data 
$extractor = new MultipleRowExtractor($crawler, $configuration); 
$data = $extractor->extract(); 
print_r($data); 

我能夠使用上述代碼獲得以下數據。

Array 
(
    [0] => Array 
     (
      [Code] => ASX 
      [hash] => 6e16c02b10a10baf739c2613bc87f906 
     ) 

    [1] => Array 
     (
      [Headline] => Initial Director's Interest Notice 
      [Published] => 10:57 AM 
      [Pages] => 1 
      [Link] => /asx/statistics/displayAnnouncement.do?display=pdf&idsId=01868833 
      [Code] => STO 
      [hash] => aa2ea9b1b9b0bc843a4ac41e647319b4 
     ) 

    [2] => Array 
     (
      [Headline] => Becoming a substantial holder 
      [Published] => 10:53 AM 
      [Pages] => 2 
      [Link] => /asx/statistics/displayAnnouncement.do?display=pdf&idsId=01868832 
      [Code] => AKG 
      [hash] => f8ff8dfde597a0fc68284b8957f38758 
     ) 

    [3] => Array 
     (
      [Headline] => LBT Investor Conference Call Business Update 
      [Published] => 10:53 AM 
      [Pages] => 9 
      [Link] => /asx/statistics/displayAnnouncement.do?display=pdf&idsId=01868831 
      [Code] => LBT 
      [hash] => cc78f327f2b421f46036de0fce270a6d 
     ) 

... 

免責聲明:我以前https://github.com/rajanrx/php-scrape框架和 我該庫的作者。你可以抓住使用簡單的捲曲以及使用 的XPath上市above.I數據希望這會有所幫助:)

+0

令人驚歎!這正是我期待的! – SilverSkin

0

CURL只能加載頁面的標記。上面的頁面使用JavaScript來加載頁面後加載數據。您可能需要使用PhantomJS或Splash。

此鏈接可能幫助:https://stackoverflow.com/a/20554152/3086531

有關讀取數據,在服務器端,我們可以使用phantomjs作爲內部PHP庫。在phantomjs中執行頁面,然後使用exec命令將數據轉儲到php中。

這篇文章有一步一步的過程來做到這一點。 http://shout.setfive.com/2015/03/30/7817/

+0

我希望的是PHP庫...我需要這個由服務器採取實時。 – SilverSkin

+0

@SilverSkin您可以在PHP中使用phantomJS庫。 本文可能有所幫助:http://shout.setfive.com/2015/03/30/7817/ – bhar1red

+0

我無法在我工作的服務器上安裝phantomJS。有沒有其他的選擇? – SilverSkin