HTML DOM อย่างง่ายส่งคืน NULL

ฉันกำลังดึงข้อมูลจากเว็บไซต์โดยใช้ตัวแยกวิเคราะห์ HTML DOM อย่างง่าย (http://simplehtmldom.sourceforge.net/ )

HTML คือ:

<tr class="productListing-odd">
    <td align="right" class="productListing-data">&nbsp;0&nbsp;</td>
    <td class="productListing-data">&nbsp;<a href="http://www.spellvault.net/p46563/Liliana-of-the-Veil/product_info.html" onmouseout="hd()" onmouseover="sd('images/101257121.jpg')">Liliana of the Veil</a>&nbsp;<br>    </td>
    <td align="center" class="productListing-data">&nbsp;Black&nbsp;</td>
    <td align="center" class="productListing-data">&nbsp;Mythic&nbsp;</td>
    <td align="center" class="productListing-data">&nbsp;Innistrad&nbsp;</td>
    <td align="right" class="productListing-data">€42,50&nbsp;</td>
    <td align="center" class="productListing-data"><input type="text" name="var[46563]" value="" size="4">&nbsp;    <span class="nowrap"><span class="template-button-left">&nbsp;</span><span class="template-button-middle"><input class="submitButton" type="submit" value="Bestel"></span><span class="template-button-right">&nbsp;</span></span>&nbsp;</td>
  </tr>

และ php:

include_once('simple_html_dom.php');

$html = file_get_html('-the url of the search query on the website-');

$array = array();
foreach($html->find('.productListing-odd, .productListing-even') as $element) {
    $row = array(
        'name' => strip_tags($element->childNodes(1)->innertext),
        'set' => strip_tags($element->childNodes(4)->innertext),
        'price' => strip_tags($element->childNodes(5)->innertext),
        'stock' => strip_tags($element->childNodes(0)->innertext)
    );
    array_push($array, $row);
}
echo json_encode($array);

ด้วยเหตุผลบางประการ ค่าของ 'price' จะส่งคืนค่า NULL ต่อไป ค่าอื่นๆ ทั้งหมดจะถูกรวบรวมอย่างเหมาะสม ฉันไม่เข้าใจว่าทำไมสิ่งนี้จึงเกิดขึ้น เนื่องจากองค์ประกอบทั้งหมดดูเหมือนจะมีโครงสร้างเดียวกัน

ขอบคุณล่วงหน้า!


person Leon    schedule 31.01.2014    source แหล่งที่มา
comment
มีเหตุผลที่คุณใช้ strip_tags($element->childNodes(1)->innertext) แทน $element->childNodes(1)->plaintext หรือไม่   -  person Enissay    schedule 01.02.2014
comment
หรือดีกว่า: $element->find('td[2] a', 0)->text()   -  person pguardiario    schedule 01.02.2014
comment
เหตุผลก็คือว่าฉันค่อนข้างใหม่กับเรื่องนี้ ขอบคุณสำหรับคำแนะนำ!   -  person Leon    schedule 02.02.2014


คำตอบ (1)


เป็นไปได้มากว่า HTML ที่คุณแยกวิเคราะห์มีชุดอักขระที่ไม่ใช่ Unicode และนี่เป็นปัญหาเนื่องจาก json_encode() ใช้ได้กับการเข้ารหัส UTF-8 เท่านั้น ข้อมูลเกือบทั้งหมดที่คุณแยกวิเคราะห์มีอักขระ ASCII ดังนั้นจึงไม่มีปัญหาใดๆ แต่ข้อมูลราคา (คอลัมน์ที่ 6) มีอักขระที่ไม่ใช่ ASCII '€' ซึ่ง json_encode() ล้มเหลว (และส่งกลับค่าว่าง)

person hindmost    schedule 31.01.2014