Home > Backend Development > PHP Tutorial > 如何用PHP抓取网站HTML

如何用PHP抓取网站HTML

WBOY
Release: 2016-06-13 12:08:38
Original
918 people have browsed it

怎么用PHP抓取网站HTML


连接地址  

http://detail.tmall.com/item.htm?spm=a230r.1.0.0.MlI5e4&id=40364502055&ad_id=&am_id=&cm_id=140105335569ed55e27b&pm_id=&abbucket=12
Copy after login



抓取上面连接的 HTML 用file_get_contents() 测试没成功 怎么回事啊?
------解决思路----------------------
file_get_contents() 成功了呀
------解决思路----------------------
<br /><?php <br />	$url="http://detail.tmall.com/item.htm?spm=a230r.1.0.0.MlI5e4&id=40364502055&ad_id=&am_id=&cm_id=140105335569ed55e27b&pm_id";<br />	$content = getcurl($url);<br />	echo $content;<br /><br />	<br />    function getcurl($url){<br />		$ch = curl_init(); <br />		curl_setopt($ch, CURLOPT_URL, $url);<br />		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);<br />		curl_setopt($ch, CURLOPT_MAXREDIRS,20);<br />		$file_contents = curl_exec($ch);<br />		return $file_contents;<br />		curl_close($ch);<br />    }<br />?><br />
Copy after login

其中curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);设置比较重要,可以用来跟随天猫的重定向页面。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template