2017-06-25 9 views
0

だから私は非常に新しいPHPです。しかし、いくつかの助けを借りて、h1 class = ____のようなタグ識別子があれば、サイトを掻き集める方法を見つけました。PHPは特定の値や数値をスクラップして出力します。

さらに、私が望む正確な単語や値を出力する方法を考え出しました。それが空白の空白で区切られている限り。たとえば、指定されたタグ名< INVENTORY>の出力が「30ボール」の場合、[0]をエコーするように指定することができ、30個しか出力されません。それは素晴らしいです。

私は問題に遭遇していますが、空白で区切られていない値を抽出しようとしていました。つまり、ソースサイトの数字が時間の経過とともに変化する可能性があるため、出力として「-34.89」を指定します(正確には、そのサイトの所在地にある数字に関係なく)。

しかし、私の出力は "-34.89dowjonesstockchange"です。空白はありません。

-34.89を出力するにはどうすればよいですか?または、その日に何処にいてもかまいません。上記の出力を表すいくつかの方法があり、exの値[0,1,2,3,4,5]のみを出力する必要があります。これは値の数で-34.89になります。

以下は、空白で決まる単語と値を出力するウェブサイト上のテスト例です。これは私が必要としているほとんどのものですが、より正確なこの方法が欠けています。

// this function is a scrapping function for ethereumchange 
function getEthereumchange(){ 
    $doc = new DOMDocument; 

    // We don't want to bother with white spaces 
    $doc->preserveWhiteSpace = false; 


    $doc->strictErrorChecking = false; 
    $doc->recover = true; 

    $doc->loadHTMLFile('https://coinmarketcap.com/'); 



    $xpath = new DOMXPath($doc); 

    $query = "//tr[@id='id-ethereum']"; 




    $entries = $xpath->query($query); 
    foreach ($entries as $entry) { 
     $result = trim($entry->textContent); 
     $ret_ = explode(' ', $result); 
     //make sure every element in the array don't start or end with blank 
     foreach ($ret_ as $key=>$val){ 
      $ret_[$key]=trim($val); 
     } 
     //delete the empty element and the element is blank "\n" "\r" "\t" 
     //I modify this line 
     $ret_ = array_values(array_filter($ret_,deleteBlankInArray)); 

     //echo the last element 
     file_put_contents(globalVars::$_cache_dir . "ethereumchange", 
$ret_[7]); 

    } 

ありがとうございました。

答えて

1

あなたが三分の一を使用したい場合利用できるパーティーライブラリhttps://github.com/rajanrx/php-scrape

<?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('https://coinmarketcap.com/'); 

// Setup configuration 
$configuration = new \Scraper\Structure\Configuration(); 
$configuration->setTargetXPath('//table[@id="currencies"]'); 
$configuration->setRowXPath('.//tbody/tr'); 
$configuration->setFields(
    [ 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Name', 
       'xpath' => './/td[2]/a', 
      ] 
     ), 
     new \Scraper\Structure\TextField(
      [ 
       'name' => 'Market Cap', 
       'xpath' => './/td[3]', 
      ] 
     ), 
     new \Scraper\Structure\RegexField(
      [ 
       'name' => '% Change', 
       'xpath' => './/td[7]', 
       'regex' => '/(.*)%/' 
      ] 
     ), 
    ] 
); 

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

wi LL以下のプリントアウト:

Array 
(
    [0] => Array 
     (
      [Name] => Bitcoin 
      [Market Cap] => $42,495,710,233 
      [% Change] => -1.09 
      [hash] => 76faae07da1d2f8c1209d86301d198b3 
     ) 

    [1] => Array 
     (
      [Name] => Ethereum 
      [Market Cap] => $28,063,517,955 
      [% Change] => -8.10 
      [hash] => 18ade4435c69b5116acf0909e174b497 
     ) 

    [2] => Array 
     (
      [Name] => Ripple 
      [Market Cap] => $11,483,663,781 
      [% Change] => -2.73 
      [hash] => 5bf61e4bb969c04d00944536e02d1e70 
     ) 

    [3] => Array 
     (
      [Name] => Litecoin 
      [Market Cap] => $2,263,545,508 
      [% Change] => -3.36 
      [hash] => ea205770c30ddc9cbf267aa5c003933e 
     ) 
    and so on ... 

を、私はそれはあなたが

免責を:)役に立てば幸い:私はこのライブラリの作者です。

1

あなただけのこれを試してみて、全体foreachセクションを削除し、その変化率を気にしている場合:ここ

$query = "//tr[@id='id-ethereum']/td[contains(@class, 'percent-24h')]"; 
$entries = $xpath->query($query); 

echo $entries->item(0)->getAttribute('data-usd'); //-5.15 

は、残りの列です:

$xpath = new DOMXPath($doc); 

$market_cap = $xpath->query("//tr[@id='id-ethereum']/td[contains(@class, 'market-cap')]"); 
echo $market_cap->item(0)->getAttribute('data-usd'); //30574084827.1 


$price = $xpath->query("//tr[@id='id-ethereum']/td/a[contains(@class, 'price')]"); 
echo $price->item(0)->getAttribute('data-usd'); //329.567 

$circulating_supply = $xpath->query("//tr[@id='id-ethereum']/td/a[@data-supply]"); 
echo $circulating_supply->item(0)->getAttribute('data-supply'); //92770467.9991 


$volume = $xpath->query("//tr[@id='id-ethereum']/td/a[contains(@class, 'volume')]"); 
echo $volume->item(0)->getAttribute('data-usd'); //810454000.0 


$percent_change = $xpath->query("//tr[@id='id-ethereum']/td[contains(@class, 'percent-24h')]"); 
echo $percent_change->item(0)->getAttribute('data-usd'); //-3.79 
+0

実際、私はこれを作って、理想的にはどの値にも適用できるようにしようとしています。出力の番号位置によって識別されます。 – Masteryogurt

+0

@ Masteryogurtあなたは、明示的にあなたが望むフィールドをターゲットにするほうがよいでしょう。明示的にテーブルに新しい列を追加すると、スクリプトは機能しなくなります。 –

+0

ええ、私はあなたが何を意味知っています。問題は、多くの場合、これらのフィールド/タグに多くのデータが含まれていることです。クリーンなものを見つけるのは非常に困難です。典型的には、上記の空白のPHP関数でターゲットをうまく乗り越えています。私は空白で見つけることができませんでした。私は時々、ソースサイトで何か変更があった場合、いくつかの数字を調整する必要があるという事実を推測します。今のところ、私はそれと一緒に暮らすことができます。 – Masteryogurt

関連する問題