Search code examples
phpweb-crawlergoutte

Trying to get same result by xpath and css element


Im trying to get the same result from a site by using dom elements and xpath. So i can make this crawler dynamic for more sites, so that i only have to fill in url and what type(xpath, domelement).

 $url = 'https://#/';
        $xpath = "/html[1]/body[1]/div[3]/header[1]/div[1]/div[1]/div[2]/div[1]/ul[1]/li[2]/ul[1]/li[1]/span[1]";        
        $client = new Client();
        $guzzleClient = new GuzzleClient(array(
            'timeout' => 60,
        ));
        $client->setClient($guzzleClient);
        $crawler = $client->request('GET', $url);
        $crawler->filter('.rate')->filter('.gold')->each(function ($node) {
        print $node->text()."\n";
        });

        $result = $crawler->filterXPath($xpath);
        var_dump($result);

result should be, gold price like this code piece outputs: $crawler->filter('.rate')->filter('.gold')->each(function ($node) { print $node->text()."\n"; });

If anything is unclear please let me know!


Solution

  • Welcome to SO.

    If you want to fetch the gold rate then you can use the below selectors.

    xpath

    //ul[@class='rates-widget list-inline']//span[@class='rate gold']
    

    CSS

    ul.rates-widget.list-inline span.rate.gold