LoginSignup
0
0

More than 5 years have passed since last update.

phpでスクライピングをやってみたが・・・・

Posted at

始めに

PHPでスクライピングをやってみたときのメモです
ちなみにやりたいことはまだできていません・・・

やりたいこと

Yahooファイナンスから値上がり率1位の銘柄を取得すること。
ちなみに、値上がり率1位の銘柄はaタグの属性'data-ylk''が'slk:yoso;pos:1'のもので取得できるようです

書いてみたソース

sample.php
<?php

$dom = DOMDocument::load('https://finance.yahoo.co.jp/');
$nodelist= $dom->getElementsByTagName('a');
$target=array_filter($nodelist,function($a){return $a->getAttribute('data-ylk')==='slk:yoso;pos:1';}); 
echo $target->nodeValue;
?>

実行結果(1回目)

Warning: DOMDocument::load(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in C:\phpsource\xpath.php on line 3

Warning: DOMDocument::load(): I/O warning : failed to load external entity "https://finance.yahoo.co.jp/" in C:\phpsource\xpath.php on line 3

がコンソールに出力された。
スタックオーバーフローを見ると、php.iniに

  • extension=php_openssl.dll
  • allow_url_include = On

が必要そう
php.iniに上記設定を追加してみる

実行結果(2回目)

php.iniにopen_sslの設定を追加した後、プログラムを実行したが

PHP Fatal error:  Uncaught Error: Call to a member function getElementsByTagName() on boolean in C:\phpsource\xpath.php:4
Stack trace:
#0 {main}
  thrown in C:\phpsource\xpath.php on line 4

Fatal error: Uncaught Error: Call to a member function getElementsByTagName() on boolean in C:\phpsource\xpath.php:4
Stack trace:
#0 {main}
  thrown in C:\phpsource\xpath.php on line 4

というエラーが出た
おそらく、phpの実装が間違っていると思わるが、どこで間違っているかは正直よくわかっていません

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0