【问题标题】:curl not showing the correct source as viewed by view page source by browsercurl 未显示通过浏览器查看页面源查看的正确源
【发布时间】:2017-02-25 20:12:00
【问题描述】:

我正在尝试学习网络抓取我选择https://www.betfair.com 作为示例,我已经成功获取了许多页面数据但是当我要访问https://www.betfair.com/sport/horse-racing 时我没有获得完整的源但是如果我查看页面源来自浏览器向我展示了数据,所以内容是由 JavaScript 或类似内容生成的。 这是我的代码:

$url ='https://www.betfair.com/sport/horse-racing';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch);
curl_close($ch);
echo $page;

如果你在浏览器查看源码时可以看到,你可以找到这个:

<a href="/sport/horse-racing?action=loadRacingSpecials&tab=SPECIALS&  modules=multipick-horse-racing" class="ui-nav link ui-clickselect ui-ga-  click" data-dimension3="sports-header" data-dimension4="Specials"   data-dimension5="Horse Racing" data-gacategory="Interface"   data-gaaction="Clicked Horse Racing Header" data-galabel="Specials"
data-loader=".multipick-content-container > div, .antepost-content-  container > div, .future-racing-content-container > div, .bet-finder-content-  container > div, .racing-specials-content-container > div, .future-racing-  market-content-container > div"
>
Specials</a>

但是 curl 没有得到这些元素。

【问题讨论】:

  • 它在 $page 结果保存到文件,你会看到结果prntscr.com/edcdny
  • @Faxsy 当我在我的本地页面上回应这个并且看到源不存在时,你能告诉我它是如何显示的吗?

标签: php curl web-scraping


【解决方案1】:

所有网站中的第一个 betfair 不接受在他们身上做蜘蛛(尽管人们经常这样做)。

我知道我是 html 的 javascript 方面的专家。但是可能会发生它是由 ajax 调用生成的。如果您使用 mozila 的 firebug 工具,您可以看到页面发出的获取数据的请求。

但我的大部分建议是使用他们拥有的 API。这是合法的,并且有一个有一些限制的免费版本。 api链接https://developer.betfair.com/

【讨论】:

  • 实际上,如果我在网站上看到它写在那里的视图页面源,那么它不是由 ajax 调用生成的。
【解决方案2】:

尝试将其保存在文件中,您会注意到您要查找的代码在那里。

    $url ='https://www.betfair.com/sport/horse-racing';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $page = curl_exec($ch);
    curl_close($ch);

    $file = fopen("1.txt","a");
    fwrite($file,$page);
    fclose($file);

【讨论】:

  • 抱歉 Faxsy 但写入文件后也没有看到代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 2020-09-15
  • 2014-04-20
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多