goutte操作

goutte操作

absolute path.
curl.cainfo = "E:/wamp/bin/php/php7.1.9/extras/ssl/cacert.pem"

2.修改php.ini文件,搜索curl.cainfo

 

 

  1. absolute path.  
  2. curl.cainfo = G:\phpStudy\WWW\cacert.pem  

 

Goutte基本用法

最近工作上用到PHP爬虫框架Goutte(号称是PHP上最好用的爬虫框架)。这里记下自己用到过的使用技巧,免得下次使用的时候再摸索。

table相关

html: <table id="ip_list">
      <tbody><tr>
  <th colspan="8"> <h2>国内高匿代理IP</h2>
      <a class="more" href="/nn">更多</a></th>
</tr>
  <tr class="subtitle">
    <th class="country">国家</th>
    <th>代理IP地址</th>
    <th>端口</th>
    <th width="20%">服务器地址</th>
    <th class="country">是否匿名</th>
    <th>类型</th>
          <th width="11%">存活时间</th>
    <th width="12%">验证时间</th>
  </tr>
  
  <tr class="odd">
    <td class="country"><img src="http://xxx.abc.com/images/flag/img.png" alt="cn"></td>
    <td>175.155.24.112</td>
    <td>808</td>
    <td>四川德阳</td>
    <td class="country">高匿</td>
    <td>HTTP</td>
      <td>3小时</td>
    <td>1分钟前</td>
  </tr>
 </tbody></table>
 
php: 
1.解析出td的内容
$crawler->filter('table#ip_list > tr')->each(function (Crawler $node, $i) {
            $ip = $node->filter('td')->each(function(Crawler $node_ip, $node_ip_num){
            $text = trim($node_ip->text());
            if (empty($text) && $node_ip_num > 0 && !empty(trim($node_ip->html()))) {
                $text = $node_ip->filter('div')->attr('title');
            }
            return $text;
            });
            return $ip;
        });
    }

2.按位置匹配td标签
$td = $crawler->filter(‘td’)->eq(1)->text();

匹配两个class

html : <div class=”class1 class2″>
php  : $crawler->filter(‘div.class1.class1’);

匹配id

html : <div id=”hello”>
php  : $crawler->filter(‘div#hello’);

图片

html : <img src=”http://www.lhzcl.com/image.png”>
php  : $crawler->filter(‘img’)->attr(‘src’);

内嵌html

这个我常用来检测匹配规则是否正确

html : <div class=”catchMeIfYouCan”><span id=”hello”>Hello</span>world</div>
php  : $crawler->filter(‘catchMeIfYouCan’)->html();

部分原创,部分参考了这篇博客

转载于:https://my.oschina.net/k9606/blog/1632463

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值