
正文
爬数据php 爬数据怎么爬
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php如何爬取天猫和淘宝商品数据
直接用Curl就行,具体爬取的数据可以穿参查看结果,方法不区分淘宝和天猫链接,但是前提是必须是PC端链接,另外正则写的不规范,所以可以自己重写正则来匹配数据。
相关问答
Q1: 用php怎么爬
其实用PHP来爬会非常方便,主要是PHP的正则表达式功能在搜集页面连接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函数非常方便的下载网页内容。
具体处理方式就是建立就一个任务队列,往队列里面插入一些种子任务和可以开始爬行,爬行的过程就是循环的从队列里面提取一个URL,打开后获取连接插入队列中,进行相关的保存。队列可以使用数组实现。
当然PHP作为但线程的东西,慢慢爬还是可以,怕的就是有的URL打不开,会死在那里。
Q2: 如何用php 编写网络爬虫
php不太适合用来写网络爬虫,因为几乎没有现成的框架,或者成熟的下载机制,也不太适合做并发处理.
下载页面的话除了一个curl,就是file_get_contents,或者curl_multi来做并发请求.curl可以代理端口,虚假ip,带cookie,带header请求目标页面,下载完成之后解析页面可以用queryList来解析html.写法类似jQuery.
提供给你我之前写的类:curl.php 希望可以帮到你.
QueryList.php和phpQuery.php由于文件太大了,没办法贴上来
?php
class Http {
public function curlRequest($url, $postData = '', $timeOut = 10, $httpHeader = array()) {
$handle = curl_init ();
curl_setopt ( $handle, CURLOPT_URL, $url );
if ($httpHeader) {
curl_setopt($handle, CURLOPT_HTTPHEADER, $httpHeader);
}
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $handle, CURLOPT_HEADER, 0 ); curl_setopt ( $handle, CURLOPT_TIMEOUT, $timeOut );
curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $handle, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'); curl_setopt ( $handle, CURLOPT_ENCODING, 'gzip,deflate,sdch');
if (! empty ( $postData )) {
curl_setopt ( $handle, CURLOPT_POST, 1 );
curl_setopt ( $handle, CURLOPT_POSTFIELDS, $postData);
}
$result['response'] = curl_exec ( $handle );
$result['httpStatus'] = curl_getinfo ( $handle, CURLINFO_HTTP_CODE );
$result['fullInfo'] = curl_getinfo ( $handle );
$result['errorMsg'] = '';
$result['errorNo'] = 0;
if (curl_errno($handle)) {
$result['errorMsg'] = curl_error($handle);
$result['errorNo'] = curl_errno($handle);
}
curl_close ( $handle );
return $result;
}
}
?
Q3: php怎么抓取其它网站数据
可以用以下4个方法来抓取网站 爬数据php的数据爬数据php:
1. 用 file_get_contents 以 get 方式获取内容:
?
$url = '';
$html = file_get_contents($url);
echo $html;
2. 用fopen打开url爬数据php,以get方式获取内容
?
$url = '';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函数,以post方式获取url
?
$data = array(
'foo'='bar',
'baz'='boom',
'site'='',
'name'='nowa magic');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' = array(
'method' = 'POST',
'header' = 'Content-type:application/x-www-form-urlencoded',
'content' = $data
//'timeout' = 60 * 60 // 超时时间(单位:s)
)
);
$url = "";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl库爬数据php,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$url = '';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
Q4: php的curl怎么爬取网页内容
创建一个新cURL资源
设置URL和相应爬数据php的选项
抓取URL并把它传递给浏览器
关闭cURL资源爬数据php,并且释放系统资源
代码案例爬数据php:
爬数据php的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于爬数据怎么爬、爬数据php的信息别忘了在本站进行查找喔。







