php爬虫非常好用的一个库

26 篇文章 0 订阅

具体库连接:

https://github.com/jae-jae/QueryList

爬虫的流程:
clipboard.png

用代理模式爬取示例1:
require 'autoload.php';
use QL\QueryList;
$urlParams = null;
$opts = [
    // Set the http proxy,设置代理模式
    'proxy' => 'socks5h://127.0.0.1:7890',
    //Set the timeout time in seconds
    'timeout' => 10,
    // Fake HTTP headers
    'headers' => [
        'Method' =>'GET',
        //'Referer' => 'https://www.google.com/',
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
        'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    ]
];

$ql = QueryList::get('https://www.google.com/search?q=QueryList',$urlParams,$opts);
var_dump($ql->getHtml());
登录示例2,用示例1的代理头部
// Get the QueryList instance
$ql = QueryList::getInstance();
// Get the login form
$form = $ql->get('https://github.com/login',$urlParams,$opts)->find('form');

// Fill in the GitHub username and password
$form->find('input[name=login]')->val('545**qq.com');
$form->find('input[name=password]')->val('****');

// Serialize the form data
$fromData = $form->serializeArray();
$postData = [];
foreach ($fromData as $item) {
    $postData[$item['name']] = $item['value'];
}

// Submit the login form
$actionUrl = 'https://github.com'.$form->attr('action');

$re = $ql->post($actionUrl,$postData,$opts);

// To determine whether the login is successful
// echo $ql->getHtml();
// exit(0);
$userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
if($userName)
{
    echo 'Login successful ! Welcome:'.$userName;
}else{
    echo 'Login failed !';
}
携带cookie访问示例3,用示例1的代理头部
$opts['headers']['Cookie'] = 'url=http%3A%2F%2Ffydj.17sysj.com%2FIndex%2Findex.html; PHPSESSID=d783b24944e3f383a87904ab4c19d525';
$ql = QueryList::get('http://fydj.17sysj.com/Index/index.html',$urlParams,$opts);
//echo $ql->getHtml();
$userName = $ql->find('.dropdown-toggle')->text();
echo $userName;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值