我正在使用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
元素后面,该元素包含特定字符串{@ 1}}。
我的php看起来像这样:
Website:
我的代码不起作用。
我的尝试$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+"
使脚本从
中提取文本答案 0 :(得分:0)
似乎contains()
是一个jquery功能而不是css选择器。使用css,您可以检查属性值,但不检查标记内的文本节点。
所以,在你的情况下,我会使用xpath选择器,尤其是:following-sibling
(参见https://stackoverflow.com/a/29380551/1997849)
答案 1 :(得分:0)
这是您问题的解决方案。
php_notes.php文件中的表。
import requests, random, re
from bs4 import BeautifulSoup as bs
url = "https://www.thenational.ae/search?q=aramco"
webpage = requests.get(url)
soup = bs(webpage.text, "html.parser")
for link in soup.find_all('h1'):
print(link.get('href'))
Crawler.php从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>
您可以从“ https://symfony.com/doc/current/components/dom_crawler.html”获得有关Symfony Crawler的帮助