Home >Backend Development >PHP Tutorial >php curl 怎么抓取天猫商品页面?

php curl 怎么抓取天猫商品页面?

WBOY
WBOYOriginal
2016-06-23 14:27:581561browse

<?php// 初始化一个 cURL 对象$curl = curl_init();  // 设置你需要抓取的URLcurl_setopt($curl, CURLOPT_URL, 'http://item.taobao.com/item.htm?id=2315770603'); // 设置headercurl_setopt($curl, CURLOPT_HEADER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 运行cURL,请求网页$data = curl_exec($curl); // 关闭URL请求curl_close($curl); // 显示获得的数据var_dump($data);?>


抓取不到页面


回复讨论(解决方案)

$ret = file_get_contents("http://item.taobao.com/item.htm?id=2315770603");var_dump($ret);

电商网站都是用了多层嵌套的 js 第三方验证技术
由于 curl 不能执行 js 代码,所以不可能抓取到有价值的页面
你可通过 v8js 扩展来实现 js 代码的执行

$url = "http://item.taobao.com/item.htm?id=2315770603";$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);curl_setopt($ch, CURLOPT_MAXREDIRS,20);$file_contents = curl_exec($ch);echo $file_contents;curl_close($ch);

抓天猫要抓到完整的信息,估计至少要几十行代码,肯定要发送多次请求才能达到目的

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn