【发布时间】: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