用xpath刮擦

时间:2017-04-28 16:25:45

标签: php xpath web-scraping scrape

我正在尝试使用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>';
}

1 个答案:

答案 0 :(得分: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)