导航:首页 > 编程语言 > PHP爬虫怎么写

PHP爬虫怎么写

发布时间:2022-08-12 00:53:58

㈠ 网页内容是由javascript或者php用爬虫有何不同

javascript是浏览器脚本,php是服务器脚本。你可以查看js的代码,但不能查看php的代码。抓取网页的时候php网页的内容显得更干净,而js网页还要过滤掉js代码。

㈡ 求一个简易的php爬虫提取网页的title

header("Content-Type: text/html; charset=gbk");
$url = "http://www..com/";
$fcontents = file_get_contents($url);
if (ereg("<title>(.*)</title>", $fcontents, $regs)){echo "ok";}else{echo "error";}
echo "<br>";
print_r($regs);

㈢ php中curl爬虫 怎么样通过网页获取所有链接

本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集。一般php采集网络数据会用file_get_contents、file和cURL。不过据说cURL会比file_get_contents、file更快更专业,更适合采集。今天就试试用cURL来获取网页上的所有链接。示例如下:

<?php
/*
* 使用curl 采集hao123.com下的所有链接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.hao123.com/');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主机部分,补全用
$host = 'http://www.hao123.com/';
if (is_array($linkarr)) {
foreach ($linkarr as $k => $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("<p>此页面的所有链接为:</p><pre>%s</pre>n", var_export($linkresult , true));
?>

function.php内容如下(即为上两篇中两个函数的合集):

<?php
function _striplinks($document) {
preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?>

㈣ 求一个PHP写的爬虫,能绕过的。

根据题主的需求,手敲两个小时代码,拿走不谢
from selenium import webdriver
import time
import os
import requests
 
 
class Huaban():
 
    def get_picture_url(self, content):
        global path
        path = "E:\spider\pictures\huaban" + '\\' + content
    
        if not os.path.exists(path):
            os.makedirs(path)
        url = "http://huaban.com"
       
        driver.maximize_window()
        driver.get(url)
        time.sleep(8)
 
       
        try:
            driver.find_elements_by_xpath('//input[@name="email"]')[0].send_keys('花瓣账号')
            print('user success!')
        except:
            print('user error!')
        time.sleep(3)
        try:
            driver.find_elements_by_xpath('//input[@name="password"]')[0].send_keys('账号密码')
            print('pw success!')
        except:
            print('pw error!')
        time.sleep(3)

㈤ 如何用PHP做网络爬虫

其实用PHP来爬会非常方便,主要是PHP的正则表达式功能在搜集页面连接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函数非常方便的下载网页内容。

㈥ php 实现网络爬虫

  1. pcntl_fork或者swoole_process实现多进程并发。按照每个网页抓取耗时500ms,开200个进程,可以实现每秒400个页面的抓取。

  2. curl实现页面抓取,设置cookie可以实现模拟登录

  3. simple_html_dom 实现页面的解析和DOM处理

  4. 如果想要模拟浏览器,可以使用casperJS。用swoole扩展封装一个服务接口给PHP层调用

在这里有一套爬虫系统就是基于上述技术方案实现的,每天会抓取几千万个页面。

㈦ 你好,我如何用php来实现网络爬虫呢具体一点

以下是访问某音乐网站,并获取其歌曲名等数组的示例,你可以参考:

<?php
header('Content-type:text/html;charset=utf-8');
$doc = file_get_contents('http://www.songtaste.com/music/');
$pa = '{MSL\((.*)\);}';
preg_match_all($pa,$doc,$r);
for($i=0;$i<count($r[1]);$i++)
{
$r1 = explode(', ',$r[1][$i]);
echo '歌曲标题:'. iconv('gb2312','utf-8',$r1[0]) .' 歌曲ID:'.$r1[1].'<br/>';
}
?>

㈧ 怎么写php爬虫自动抓取百度知道

curl来写。模拟登陆。抓取页面。分析标签。正则匹配你想要的内容。然后存入数据大概就是这样的流程。

阅读全文

与PHP爬虫怎么写相关的资料

热点内容
又见金底副图选股公式源码 浏览:203
烧录编程器那个好用 浏览:542
三晋先锋app如何签约 浏览:439
网络如何读取服务器信息 浏览:434
mac压缩解压视频 浏览:906
这就是程序员魅力 浏览:296
京东java算法笔试题 浏览:178
柱子加密箍筋不准有接头 浏览:199
我的世界服务器菜单插件如何使用 浏览:12
刘毅10000词pdf 浏览:890
刚毕业的程序员会什么 浏览:974
单片机控制64路开关量 浏览:982
win10截图编程 浏览:420
怎样把名字变成文件夹 浏览:203
文件怎么搞成文件夹 浏览:730
多线程编程php 浏览:606
安卓机越用越卡有什么办法 浏览:17
高中生解压操场适合做的游戏 浏览:395
程序员java招聘 浏览:462
未来之光手机云服务器 浏览:160