Php 用痛风扫桌子
我使用的是Php 用痛风扫桌子,php,goutte,Php,Goutte,我使用的是PHP7.1.33和“fabbot/goutte”:“^4.0” 我正在尝试解析从html页面获得的表。我想有公司名称和链接 运行脚本时,我得到:Nodelist empty 下面是我的最低可行示例: const URL = 'https://www.marketbeat.com/ratings/by-issuer'; $firmArr = array(); $firm = [ 'name' => nul
PHP7.1.33
和“fabbot/goutte”:“^4.0”
我正在尝试解析从html页面获得的表。我想有公司名称和链接
运行脚本时,我得到:Nodelist empty
下面是我的最低可行示例:
const URL = 'https://www.marketbeat.com/ratings/by-issuer';
$firmArr = array();
$firm = [
'name' => null,
'sourceTEXT' => null,
'sourceURL' => null,
];
$client = new Client();
$content = $client->request('GET', self::URL)->html();
$crawler = new Crawler($content, null, null);
$table = $crawler->filter('#form1 > table')->first()->closest('table');
$table->filter('tr')
->each(function (Crawler $tr) use (&$firm, &$firmArr) {
// name
$name = $tr->filter('td:nth-child(1)')->text(); // nodelist ist emtpy
$firm['name'] = $name;
// link
$TEMP = [];
$tr->filter('td:nth-child(1) > a')->each(function ($link) use (&$TEMP) {
array_push($TEMP, $link->text(), $link->attr('href'));
});
$firm['sourceTEXT'] = $TEMP[0];
$firm['sourceURL'] = $TEMP[1];
// add everything to result array
array_push($firmArr, $firm);
});
return $firmArr;
当尝试获取$name
时,我会在下面一行获取-$name=$tr->filter('td:nth child(1)))->text()代码>-该节点列表是emtpy
有什么建议我做错了什么
谢谢你的回复 我认为“#form1>表”
选择器不会返回任何内容,因为表是表单下的几个div。试试“#form1 table”
。此外,表中的第一个tr
没有任何td
元素,它有th
s.@solarc Thx!如何跳过第一个元素?在查找名称之前,可以在匿名函数中检查是否有th
,如果有,只需返回return代码>@solarc-Thx!你能提供一个有效的例子吗?