douxitao8170 2016-01-30 14:58
浏览 44

尝试使用PHP从html页面中提取信息

I'd like to know how to do the following. I have an html page with this part of code:

<div id="sportSelect" class="longSelect">
                <select name="ctl00$ContentPlaceViewResults$MedalistResultsSearchSmallBlock$ResultsSearchSmallBlock$SportsListing" id="ctl00_ContentPlaceViewResults_MedalistResultsSearchSmallBlock_ResultsSearchSmallBlock_SportsListing" class="dropdownfilter">
        <option value="">Choose Sport*</option>
        <option selected="selected" value="31766">Archery</option>
        <option value="32588">Athletics</option>
        <option value="31753">Badminton</option>
        <option value="126990">Baseball</option>
        <option value="31341">Basketball</option>
</div>

Now I implemented this code:

<html>

<body>

<?php
// Include the library
include('simple_html_dom.php');

$html=file_get_html('http://www.olympic.org/olympic-results/london-2012/archery');
foreach($html->find('div') as $element) {
    if ($element->id == 'sportSelect')
    {
        $v =  $element->find('option[selected]', 0)->value . '</br>'; 
    }
}

echo $v

?>
</body>
</html>

This is ok, it works because I get the value 31766 (the code corrisponding to Archery). Actually I would want to get "Archery" in the v variable and not 31766.

  • 写回答

1条回答 默认 最新

  • dongzichan2886 2016-01-30 15:07
    关注

    What I see from http://simplehtmldom.sourceforge.net/manual.htm is that you should use something similar to $element->find('option[selected]', 0)->plaintext to get the node's text content instead of its value attribute.

    The best tip I can give you when discovering a new language / library is to read the documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数