【问题标题】:Simple html dom - other result than expected简单的 html dom - 其他结果超出预期
【发布时间】:2013-10-09 22:11:41
【问题描述】:

我尝试使用 simple_html_dom 从网页中检索信息,如下所示:

<?PHP
include_once('dom/simple_html_dom.php');
$urlpart="http://w2.brreg.no/motorvogn/";
$url = "http://w2.brreg.no/motorvogn/heftelser_motorvogn.jsp?regnr=BR15597";
$html = file_get_html($url);

foreach($html->find('a') as $element) 
       if(preg_match('*dagb*',$element)) {
       $result=$urlpart.$element->href;

       $resultcontent=file_get_contents($result);
       echo $resultcontent;

       }

?>

$result 变量首先给了我这个 URL: http://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0&regnr=BR15597

当用我的浏览器访问上面的 URL 时,我得到了我期望的内容。

当使用 $resultcontent 检索内容时,我得到了不同的结果,它在挪威语中显示为“无效输入”。

任何想法为什么?

【问题讨论】:

  • 哦,天哪,这很难,我尝试了很多方法来下载页面,但仍然遇到同样的错误......奇怪的是,当你用预期的链接替换 ​​$result 时,它显示正确的输出!!!所以它可能在提取的链接中有一些隐藏的字符,我个人找不到任何 :-/ 真是个难题 xD
  • 这是一个艰难的过程,但通过下面的答案解决了!感谢您提供帮助!

标签: php web-scraping simple-html-dom


【解决方案1】:
foreach($html->find('a') as $element) 
       if(preg_match('*dagb*',$element)) {
       $result=$urlpart.$element->href;
       $resultcontent=file_get_contents(html_entity_decode($result));
       echo $resultcontent;

       }

这应该可以解决问题。

【讨论】:

    【解决方案2】:

    问题在于您的 URL 查询参数。

    http://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0&regnr=BR15597
    

    URL 中的字符串 '&reg' 将在 file_get_contents 函数中转换为 Symbol ®,从而阻止您获得实际结果。

    您可以在第 #11 行使用html_entity_decode 函数

    $resultcontent=file_get_contents(html_entity_decode($result));
    

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 2014-10-04
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      相关资源
      最近更新 更多