我正在使用https://github.com/FriendsOfPHP/Goutte解析和提取数据,而且我做得很好.
但现在我偶然发现了一个有点不友好的地方:
<tr>
<th>Website:</th>
<td>
<a href="http://www.adres.com" target="_blank">http://www.adres.com</a>
</td>
</tr>我试图从td元素中获取文本,该元素紧跟在th元素之后,该元素包含特定的字符串,在本例中为Website:。
我的php如下所示:
$client3 = new \Goutte\Client();
$crawler3 = $client3->request('GET', $supplierurl . 'contactinfo.html');
if($crawler3->filter('th:contains("+Website+") + td a')->count() > 0) {
$parsed_company_website_url = $crawler3->filter('th:contains("Website:") + td')->text();
} else {
$parsed_company_website_url = null;
}
return $parsed_company_website_url;问题
我的代码不起作用。
我的尝试
"+Website+"和"Website:"去做
使脚本从
发布于 2020-03-21 15:26:24
这是你问题的解决办法。
php_notes.php文件中的表。
<table id="table" border="1">
<tr>
<a href="">xyz</a>
<a href="">abc</a>
<h1>Heading</h1>
<th>Website:</th>
<td>
<a href="http://www.adres.com" target="_blank">http://www.adres.com</a>
</td>
<th>Website:abc</th>
<td>
<a href="http://www.adres.com" target="_blank">http://www.ares.com</a>
</td>
</tr>
</table>Crawler.php从php_notes.php文件中找到锚标记中的文本。
use Weidner\Goutte\GoutteFacade;
use Symfony\Component\DomCrawler\Crawler;
$crawler = GoutteFacade::request('GET','http://localhost/php_notes.php');
$table = $crawler->filter('#table'); // find the parent table
// find each td tag
$tdText = $table->filter('td')->each(function ($node){
$alike = $node->previousAll(); // calculate the elements of the same level above this
//element :Will return array containing the tags above this tag.
// dump('Size of array => '.sizeof($alike));
$elementTag = $alike->eq(0); // find the tag above this <td> tag.
// if the tag above this tag is a <th> tag
if($elementTag->nodeName()=='th'){
if($elementTag->text()=='Website:')
{
$text = $node->filter('a')->text();
dd('Text found form td "'.$text.'"');
}
}
});
dd('Not Text Was Found From A tag');您可以从这里“crawler.html”获得有关Symfony Crawler的帮助。
https://stackoverflow.com/questions/45796228
复制相似问题