美文网首页
Goutte基本用法

Goutte基本用法

作者: elileo | 来源:发表于2020-08-24 23:40 被阅读0次

最近工作上用到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://blog.csdn.net/weixin_33971130/article/details/89063210

相关文章

  • Goutte基本用法

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

  • 定时器

    setTimeout和clearTimeout基本用法 setInterval和clearInterval基本用法...

  • 2019-11-16

    E战到底DAY14 SUMIF和SUMIFS函数 一.基本用法 SUMIF基本用法 SUMIFS基本用法 SUMI...

  • 11 - 动态数据绑定实现原理

    一、defineProperty 基本用法 1、基本写法: 2、参数 3、descriptor 参数的基本用法 1...

  • as 基本用法

    插件安装 plugin auto import 相当于 eclipse ctrl+o 或者as alt+enter...

  • 基本用法

    Installation 安装 npm install vue vue-server-renderer --sav...

  • 基本用法

    html css js

  • 基本用法

    本地与远程:push 命令会把本地仓库推送到远程仓库(比如gitbub,码云)在push之前要与某个远程仓库建立连...

  • 基本用法

    TensorFlow使用图(graph)表示计算任务,图中的节点被称为op。一个Tensor一般为一个类型化的多维...

  • "?.","??","??=","!"基本用法

    空值合并操作符( ?? )ES2020 const a = b ?? c; // 解释为 如果b为null或un...

网友评论

      本文标题:Goutte基本用法

      本文链接:https://www.haomeiwen.com/subject/yioojktx.html