質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

2697閲覧

php getElementsByTagNameでの抽出

Koh_

総合スコア27

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

1クリップ

投稿2018/03/17 05:48

PHP:DomDocumentのGetElementsByTagNameを用いて指定したタグの情報を抽出しようとしています。
指定したタグが存在しない場合のエラー処理をどのようにすればよいか、ご教授ください。

対象とするHTMLの中には同一名のタグ <lowestofferlisting>が複数あります。このタグ中の
<landedprice>中の<Amount>の内容
<listingprice>中の<Amount>の内容
<shipping>中の<Amount>の内容
を取ってくるのが目的です。ただし、今回一例として以下に示したHTMLは<lowestofferlisting>が4個ありますが、本来対象とするHTML中の<lowestofferlisting>の数は0~4個です。

HTMLは以下です。<lowestofferlisting>が4つあります。

html

1<getlowestofferlistingsforasinresult asin="430946209X" status="Success"> 2 <allofferlistingsconsidered>true</allofferlistingsconsidered> 3 <product> 4 <identifiers> 5 <marketplaceasin> 6 <marketplaceid>A1VC38T7YXB528</marketplaceid> 7 <asin>430946209X</asin> 8 </marketplaceasin> 9 </identifiers> 10 <lowestofferlistings> 11 <lowestofferlisting> 12 <qualifiers> 13 <itemcondition>Used</itemcondition> 14 <itemsubcondition>Acceptable</itemsubcondition> 15 <fulfillmentchannel>Merchant</fulfillmentchannel> 16 <shipsdomestically>True</shipsdomestically> 17 <shippingtime> 18 <max>0-2 days</max> 19 </shippingtime> 20 <sellerpositivefeedbackrating>95-97%</sellerpositivefeedbackrating> 21 </qualifiers> 22 <numberofofferlistingsconsidered>1</numberofofferlistingsconsidered> 23 <sellerfeedbackcount>95329</sellerfeedbackcount> 24 <price> 25 <landedprice> 26 <currencycode>JPY</currencycode> 27 <amount>512.00</amount> 28 </landedprice> 29 <listingprice> 30 <currencycode>JPY</currencycode> 31 <amount>164.00</amount> 32 </listingprice> 33 <shipping> 34 <currencycode>JPY</currencycode> 35 <amount>348.00</amount> 36 </shipping> 37 </price> 38 <multipleoffersatlowestprice>False</multipleoffersatlowestprice> 39 </lowestofferlisting> 40 <lowestofferlisting> 41 <qualifiers> 42 <itemcondition>Used</itemcondition> 43 <itemsubcondition>Good</itemsubcondition> 44 <fulfillmentchannel>Merchant</fulfillmentchannel> 45 <shipsdomestically>True</shipsdomestically> 46 <shippingtime> 47 <max>0-2 days</max> 48 </shippingtime> 49 <sellerpositivefeedbackrating>95-97%</sellerpositivefeedbackrating> 50 </qualifiers> 51 <numberofofferlistingsconsidered>3</numberofofferlistingsconsidered> 52 <sellerfeedbackcount>8689</sellerfeedbackcount> 53 <price> 54 <landedprice> 55 <currencycode>JPY</currencycode> 56 <amount>544.00</amount> 57 </landedprice> 58 <listingprice> 59 <currencycode>JPY</currencycode> 60 <amount>287.00</amount> 61 </listingprice> 62 <shipping> 63 <currencycode>JPY</currencycode> 64 <amount>257.00</amount> 65 </shipping> 66 </price> 67 <multipleoffersatlowestprice>False</multipleoffersatlowestprice> 68 </lowestofferlisting> 69 <lowestofferlisting> 70 <qualifiers> 71 <itemcondition>Collectible</itemcondition> 72 <itemsubcondition>Good</itemsubcondition> 73 <fulfillmentchannel>Merchant</fulfillmentchannel> 74 <shipsdomestically>True</shipsdomestically> 75 <shippingtime> 76 <max>0-2 days</max> 77 </shippingtime> 78 <sellerpositivefeedbackrating>95-97%</sellerpositivefeedbackrating> 79 </qualifiers> 80 <numberofofferlistingsconsidered>3</numberofofferlistingsconsidered> 81 <sellerfeedbackcount>2532</sellerfeedbackcount> 82 <price> 83 <landedprice> 84 <currencycode>JPY</currencycode> 85 <amount>832.00</amount> 86 </landedprice> 87 <listingprice> 88 <currencycode>JPY</currencycode> 89 <amount>575.00</amount> 90 </listingprice> 91 <shipping> 92 <currencycode>JPY</currencycode> 93 <amount>257.00</amount> 94 </shipping> 95 </price> 96 <multipleoffersatlowestprice>False</multipleoffersatlowestprice> 97 </lowestofferlisting> 98 <lowestofferlisting> 99 <qualifiers> 100 <itemcondition>Used</itemcondition> 101 <itemsubcondition>Good</itemsubcondition> 102 <fulfillmentchannel>Merchant</fulfillmentchannel> 103 <shipsdomestically>True</shipsdomestically> 104 <shippingtime> 105 <max>8-13 days</max> 106 </shippingtime> 107 <sellerpositivefeedbackrating>90-94%</sellerpositivefeedbackrating> 108 </qualifiers> 109 <numberofofferlistingsconsidered>1</numberofofferlistingsconsidered> 110 <sellerfeedbackcount>894</sellerfeedbackcount> 111 <price> 112 <landedprice> 113 <currencycode>JPY</currencycode> 114 <amount>6651.00</amount> 115 </landedprice> 116 <listingprice> 117 <currencycode>JPY</currencycode> 118 <amount>5945.00</amount> 119 </listingprice> 120 <shipping> 121 <currencycode>JPY</currencycode> 122 <amount>706.00</amount> 123 </shipping> 124 <points> 125 <pointsnumber>5</pointsnumber> 126 <pointsmonetaryvalue> 127 <currencycode>JPY</currencycode> 128 <amount>5.00</amount> 129 </pointsmonetaryvalue> 130 </points> 131 </price> 132 <multipleoffersatlowestprice>False</multipleoffersatlowestprice> 133 </lowestofferlisting> 134 </lowestofferlistings> 135 </product> 136 </getlowestofferlistingsforasinresult> 137

上のHTMLを対象とすると、以下のphpコードで抽出することができました。HTMLは$domの中に入っています。
しかし以下のコードだと、<lowestofferlisting>が4つ未満の場合エラーが出て処理が止まってしまいます(本来対象とするHTMLの<lowestofferlisting>の数は0~4個なので)。例えば、<lowestofferlisting>が3個の場合、$root_0_3の参照でエラーが出ます。

php

1$root_0 = $dom_->getElementsByTagName("GetLowestOfferListingsForASINResult")->item(0); 2 3$root_0_0 = $root_0->getElementsByTagName("LowestOfferListing")->item(0); #複数ある中の最初の<lowestofferlisting>を選択 4$Amount0_1 = $root_0_0->getElementsByTagName("Amount")->item(0); #<landedprice>中の<Amonut>を取得 5$Amount0_2 = $root_0_0->getElementsByTagName("Amount")->item(1); #<listingprice>中の<Amonut>を取得 6$Amount0_3 = $root_0_0->getElementsByTagName("Amount")->item(2); #<shipping>中の<Amonut>を取得 7 8$root_0_1 = $root_0->getElementsByTagName("LowestOfferListing")->item(1); #複数ある中の2番目の<lowestofferlisting>を選択 9$Amount1_1 = $root_0_1->getElementsByTagName("Amount")->item(0); #<landedprice>中の<Amonut>を取得 10$Amount1_2 = $root_0_1->getElementsByTagName("Amount")->item(1); #<listingprice>中の<Amonut>を取得 11$Amount1_3 = $root_0_1->getElementsByTagName("Amount")->item(2); #<shipping>中の<Amonut>を取得 12 13$root_0_2 = $root_0->getElementsByTagName("LowestOfferListing")->item(2); #複数ある中の3番目の<lowestofferlisting>を選択 14$Amount2_1 = $root_0_2->getElementsByTagName("Amount")->item(0); #<landedprice>中の<Amonut>を取得 15$Amount2_2 = $root_0_2->getElementsByTagName("Amount")->item(1); #<listingprice>中の<Amonut>を取得 16$Amount2_3 = $root_0_2->getElementsByTagName("Amount")->item(2); #<shipping>中の<Amonut>を取得 17 18$root_0_3 = $root_0->getElementsByTagName("LowestOfferListing")->item(3); #複数ある中の4番目の<lowestofferlisting>を選択 19$Amount3_1 = $root_0_3->getElementsByTagName("Amount")->item(0); #<landedprice>中の<Amonut>を取得 20$Amount3_2 = $root_0_3->getElementsByTagName("Amount")->item(1); #<listingprice>中の<Amonut>を取得 21$Amount3_3 = $root_0_3->getElementsByTagName("Amount")->item(2); #<shipping>中の<Amonut>を取得 22

エラー文

Fatal error: Uncaught Error: Call to a member function getElementsByTagName() on null in...

$root_0_3の参照でnullとなっているためエラーが出たと考え、回避するようにif文を入れてみましたが、同様のエラーが出ました。どのようにすればエラー回避できるのでしょうか。どなたかご教授ください。

php

1if (!empty($root_0->getElementsByTagName("LowestOfferListing")->item(3))) { 2$root_0_3 = $root_0->getElementsByTagName("LowestOfferListing")->item(3); 3$Amount3_1 = $root_0_3->getElementsByTagName("Amount")->item(0); 4$Amount3_2 = $root_0_3->getElementsByTagName("Amount")->item(1); 5$Amount3_3 = $root_0_3->getElementsByTagName("Amount")->item(2); 6} else { 7echo "No data found"; 8}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

指定したタグが存在しない場合のエラー処理をどのようにすればよいか、ご教授ください。

_0 _1 とか書いている時点でアンチパターンだと思ってください。こういうのはループ処理で書くべきです。

また,質問の方針通りに回答してもいいですが,そもそもXPath使ったほうが圧倒的にラクなので方針変えましょう。

PHPネイティブのDOMによるスクレイピング入門 - Qiita

とりあえずこれを一通りごらんになって,ソースコードを書きなおしてみてください。

投稿2018/03/17 06:00

mpyw

総合スコア5223

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問