2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpqueryでaタグ内のリンク情報を取得する

Posted at

  <?php
    require_once("./phpQuery-onefile.php");
    $html = file_get_contents("https://ja.wikipedia.org/wiki/%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0");

    #ここでhtml全文を取得している
    $phpobj = phpQuery::newDocument($html);

    #aタグの取得
    #深い場所の子要素の場合、慎重に指定する
    $links = $phpobj["#mw-content-text > .mw-parser-output > ul > li > a"];


    #$linksはaタグの情報が入っているので、forの中でhrefの情報を取得する
    #attr("href")をtext()に変更すれば、aタグに囲まれたテキストを取得できる
    foreach ($links as $link) {
      echo pq($link)->attr("href")."<br>";
      echo pq($link)->text()."<br>";
      echo "----------------<br>";
    }
  ?>
2
3
0

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?