使用xpath抓取

问题描述 投票:0回答:1

我正在尝试使用 DOMXPath 获取搜索结果的项目。以下是包含 4 个结果元素的搜索结果的示例:

https://www.11880.com/suche/0521441422/deutschland

现在使用下面的代码我得到了 4 个元素,但我不知道如何获取元素内的每个项目(如名称、街道等)。

你能帮我吗?

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.11880.com/suche/0521441422/deutschland'); 
$page = curl_exec($ch);

$dom = new DOMDocument();
$dom->loadHTML($page);
$xpath = new DOMXPath($dom);

foreach($xpath->query('//li[@class="search-result-list-item"]') as $item)
{
    echo trim($item->nodeValue).'<br>';
}
php web-scraping xpath
1个回答
0
投票

我想你可以使用:

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.11880.com/suche/0521441422/deutschland');
$page = curl_exec($ch);

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($page);
$xpath = new DOMXPath($dom);

$results = [];
$results['name'] = $xpath->query('//h2[@itemprop="name"]');
$results['street-name'] = $xpath->query('//span[@class="street-address"]');
$results['postal-code'] = $xpath->query('//span[@class="postal-code"]');
$results['address-locality'] = $xpath->query('//span[@class="address-locality"]');
$results['quarter'] = $xpath->query('//span[@class="quarter"]');

for($x=0; $x < $results['name']->length;$x++)
{
    print trim($results['name']->item($x)->textContent);
    print trim($results['street-name']->item($x)->textContent);
    print trim($results['postal-code']->item($x)->textContent);
    print trim($results['address-locality']->item($x)->textContent);
    print trim($results['quarter']->item($x)->textContent)."\n";
}

输出:

Brauer, Harald Dr.med. Hals- Nasen- OhrenarztHauptstr. 9233647Bielefeld(Brackwede)
Elies-Kramme, Sybille Dr.med. Fachärztin für HNO-HeilkundeHauptstr. 9233647Bielefeld(Brackwede)
Gemeinschaftspraxis HNO-Bielefeld-Süd ÜBAG Michael K.W. Stolle und Dr. Sybille Elies-KrammeHauptstr. 9233647Bielefeld(Brackwede)
Stolle, Michael K. W. Facharzt für HNO-HeilkundeHauptstr. 9233647Bielefeld(Brackwede)
© www.soinside.com 2019 - 2024. All rights reserved.