【问题标题】:PHP scrape data from websitePHP从网站上抓取数据
【发布时间】:2016-03-25 08:41:08
【问题描述】:

我是编程新手。所以我选择使用 Wordpress 构建网页。但是我正在尝试从其他站点收集天气数据,我找不到合适的插件来抓取数据,因此决定尝试一下并自己整理一些东西。 但是由于我对编程的有限理解给了我一些问题。从网络上得到一点灵感,我把它放在一起:

$html = file_get_contents('http://www.frederikshavnhavn.dk/scripts/weatherwindow.php?langid=2'); //get the html returned from the following url

$poke_doc = new DOMDocument();

libxml_use_internal_errors(false); //disable libxml errors

if(!empty($html)){ //if any html is actually returned

  $poke_doc->loadHTML($html);
  libxml_clear_errors(); //remove errors for yucky html

  $poke_xpath = new DOMXPath($poke_doc);

  //get all the spans's with an id
  $poke_type = $poke_xpath->query("//span[@class='weathstattype']");
  $poke_text = $poke_xpath->query("//span[@class='weathstattext']");

  foreach($poke_text as $text){ 
    foreach($poke_type as $type){
    echo $type->nodeValue;
    echo $text->nodeValue . "</br>";
    continue 2;
   } 
  break;
 }    
} 

这对我来说是全新的,我真的很想让它为我工作,希望能更好地理解作品背后的代码。

我想要实现的是带有数据的格式化列表。 1. 价值 $type $text 2. 价值 $type $text

现在它给我带来了很多麻烦。 当我使用 continue 2 时,它不会更改值 $type,但是当我只使用 continue 语句时,它会更改 $type 但不会更改 $text。我怎样才能让它每次都改变这两个值?

感谢您的帮助。

【问题讨论】:

  • 您应该添加var_dump($poke_type);var_dump($poke_text); 的输出。这些至少包含您需要的所有信息吗?
  • 是的,他们应该提供所有信息。但我不确定为什么需要 var_dump($poke_type);和 var_dump($poke_text); ?或者放在哪里,因为我看到他们只给了我几个错误?
  • 您可能想试试Goutte。这是你如何使用它stackoverflow.com/questions/15628926/…

标签: php


【解决方案1】:

尝试添加此方法:

function get_inner_html( $node ) {
    $innerHTML= '';
    $children = $node->childNodes;
    foreach ($children as $child) {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }

    return $innerHTML;
} 

然后用这个替换foreach:

  foreach($poke_text as $text){ 
     //echo $type ->nodeValue . "</n>";
      echo get_inner_html($text ).'<br>';

  }  
    foreach($poke_type as $type){
     //echo $text ->nodeValue;
     echo get_inner_html($type ).'<br>';
  }

产生这个:

  1. 197° (悉尼) 5.7 °C 斯蒂根德 4.8 m/s 斯蒂根德 5.4 m/s 斯蒂根德 -6 cm Faldende 1004 hPa Vindretning Lufttemperatur Middel vindhastighed Max vindhastighed Vandstand Lufttryk

【讨论】:

    【解决方案2】:

    好友在您的代码中,您的 foreach 循环(最后)您使用 $type 作为 $text 和 $text 作为 $type.. 我运行代码并更改变量,因为它们应该可以正常工作..

    $html = file_get_contents('http://www.frederikshavnhavn.dk/scripts/weatherwindow.php?langid=2'); //get the html returned from the following url
    
    $poke_doc = new DOMDocument();
    
    libxml_use_internal_errors(false); //disable libxml errors
    
    if(!empty($html)){ //if any html is actually returned
    
      $poke_doc->loadHTML($html);
      libxml_clear_errors(); //remove errors for yucky html
    
      $poke_xpath = new DOMXPath($poke_doc);
    
      //get all the spans's with an id
      $poke_type = $poke_xpath->query("//span[@class='weathstattype']");
    
      $poke_text = $poke_xpath->query("//span[@class='weathstattext']");
    
      foreach($poke_text as $text){ 
         echo $text->nodeValue;
      }  
        foreach($poke_type as $type){
         echo $type->nodeValue;
      }
    }
    

    这是我从你的代码中得到的结果(通过更改循环中的变量)

    196° (Syd) 5.6 °C 4.1 m/s 5 m/s -6 cm 1004 hPa Vindretning Lufttemperatur Middel vindhastighed Max vindhastighed Vandstand Lufttryk

    现在你有了你的数据,我想你可以管理如何对它们进行排序......

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 2013-05-21
      • 2014-07-06
      • 1970-01-01
      相关资源
      最近更新 更多