QueryList v4 如何获取html标签自定义属性的值。

yulinzhihou
  • 2
新手上路,请多包涵
<dd class="server-and-time">
    <span class="server-info" data-wordId="123" title="">加载中...</span>
    <p class="time">123212412</p>
</dd>
<dd class="server-and-time">
    <span class="server-info" data-wordId="234" title="">加载中...</span>
    <p class="time">123212412</p>
</dd>
<dd class="server-and-time">
    <span class="server-info" data-wordId="345" title="">加载中...</span>
    <p class="time">123212412</p>
</dd>
<dd class="server-and-time">
    <span class="server-info" data-wordId="567" title="">加载中...</span>
    <p class="time">123212412</p>
</dd>
<dd class="server-and-time">
    <span class="server-info" data-wordId="789" title="">加载中...</span>
    <p class="time">123212412</p>
</dd>

我使用的方法抓取不到data-worldId对应的值

public function index(QueryList $queryList)
{
    $client = new Client();
    $res = $client->request('GET', 'http://www.xxxxxx.com', [
        'headers' => [
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
            'Accept-Encoding' => 'gzip, deflate, br',
        ]
    ]);

    $html = (string)$res->getBody();
    $currentListRoleZone = $queryList->html($html)->find('.server-info')->attrs('data-wordId');
    var_dump($currentListRoleZone);
}

输出结果

object(Tightenco\Collect\Support\Collection)[512]
  protected 'items' => 
    array (size=20)
      0 => null
      1 => null
      2 => null
      3 => null
      4 => null
回复
阅读 432
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏