【问题标题】:Why can I not scrape the title off this site?为什么我不能从这个网站上刮掉标题?
【发布时间】:2012-07-12 21:29:05
【问题描述】:

我正在使用 simple-html-dom 从指定网站上刮取标题。

<?php

include('simple_html_dom.php');

$html = file_get_html('http://www.pottermore.com/');

foreach($html->find('title') as $element) 
       echo $element->innertext . '<br>';

?>

我尝试过的任何其他网站都可以,例如 apple.com。

但是如果我输入pottermore.com,它不会输出任何东西。 Pottermore 上面有 Flash 元素,但我试图刮掉标题的主屏幕没有 Flash,只有 html。

【问题讨论】:

  • var_dump($html-&gt;find('title')); 输出什么?你能把所说的内容放在 OP 中吗(如果不是太长,有时 simple-html-dom 的输出是)
  • 也许 pottermore 有防刮保护,你得到的不是 html 的东西(例如什么都没有)。
  • 也许该站点正在检查用户代理字符串等?您确定 HTML 下载正确吗?

标签: php html simple-html-dom scraper


【解决方案1】:

这对我有用:)

$url = 'http://www.pottermore.com/';
$html = get_html($url);
file_put_contents('page.htm',$html);//just to test what you have downloaded
echo 'The title from: '.$url.' is: '.get_snip($html, '<title>','</title>');

function get_html($url)
{
    $ch = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; //browsers keep this blank.  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows;U;Windows NT 5.0;en-US;rv:1.4) Gecko/20030624 Netscape/7.1 (ax)');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE); 
    $result = curl_exec ($ch);
    curl_close ($ch);
    return($result);
}

function get_snip($string,$start,$end,$trim_start='1',$trim_end='1')
{
    $startpos = strpos($string,$start);
    $endpos = strpos($string,$end,$startpos);

    if($trim_start!='')
    {
        $startpos += strlen($start);
    }
    if($trim_end=='')
    {
        $endpos += strlen($end);
    }
    return(substr($string,$startpos,($endpos-$startpos)));
}

【讨论】:

  • cURL + 并不能解决实际问题,那就是找出页面标题。
  • 抱歉,我已经正确阅读了你的问题:(|) - 请参阅上面的编辑
  • 谢谢!不过,根据 get_snip 真正解决问题的方法,我仍然有点困惑。
  • get_snip() 是一个获取 之间文本的函数我已经测试过它并且它有效:)
【解决方案2】:

只是为了确认其他人在说什么,如果您不发送用户代理字符串,本网站将发送 403 Forbidden。

添加这个对我有用:

用户代理:Mozilla/5.0 (Windows;U;Windows NT 5.0;en-US;rv:1.4) Gecko/20030624 Netscape/7.1 (ax)

【讨论】:

    【解决方案3】:

    函数file_get_html 在幕后使用file_get_contents。此函数可以从 URL 中提取数据,但要这样做,它会发送一个用户代理字符串。

    默认情况下,此字符串为空。一些网络服务器利用这一事实来检测非浏览器正在访问其数据并选择禁止此操作。

    您可以在 php.ini 中设置user_agent 来控制发送的用户代理字符串。或者,您可以尝试:

    ini_set('user_agent','UA-String');
    

    'UA-String' 设置为您喜欢的任何值。

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2011-10-28
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多