get hidden input tag value as string using PHP sim

2019-04-12 06:21发布

问题:

I am trying to get the input type hidden tag values (CAS, AH, 11, etc.) along with the name attribute but all I get is a blank page upon running my PHP based parser. Anybody know what's wrong? I already checked Grabbing hidden inputs as a string (Using PHP Simple HTML DOM Parser) but it was of no help.

Block of html I need to iterate through:

<td valign="bottom" align="center">
<input type="hidden" value="CAS" name="College">
<input type="hidden" value="AH" name="Dept">
<input type="hidden" value="111" name="Course">
<input type="hidden" value="J1" name="Section">
<input type="hidden" value="" name="Subject">
<input type="hidden" value="" name="MtgDay">
<input type="hidden" value="" name="MtgTime">

My solution that displays nothing:

<?php
include('simple_html_dom.php'); 

  $seed = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1346752597?ModuleName=univschr.pl&SearchOptionDesc=Class+Subject&SearchOptionCd=C&KeySem=20133&ViewSem=Fall+2012&Subject=&MtgDay=&MtgTime=';
  web_scrape($seed);

  function web_scrape($url)
{
    $data = new simple_html_dom();  
    $data->load_file($url);
    $nodes = $data->find("/html/body/form/center/table/tbody/tr/td[2]/input[type=hidden]");

    foreach ($nodes as $node) {
    $val = $node->value;
    echo $val;
    }

}

 ?>

回答1:

I tried the code and this works for me, I think your $data->find is not correct:

include('../simple_html_dom.php');


  $seed = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1346752597?ModuleName=univschr.pl&SearchOptionDesc=Class+Subject&SearchOptionCd=C&KeySem=20133&ViewSem=Fall+2012&Subject=&MtgDay=&MtgTime=';
  web_scrape($seed);

  function web_scrape($url)
{
   $data = file_get_html($url);
    //$data = new simple_html_dom();  
   // $data->load_file($url);
    $nodes = $data->find("input[type=hidden]");

    foreach ($nodes as $node) {
    $val = $node->value;
    echo $val . "<br />";
    }

}