Reputation: 759
I'm trying to access a table on a web page using DomCrawler and I'm not sure I'm using the right approach. The example below points to a Yahoo site and uses the XPath of the target table. Ideally I will loop through the s for the data, but for now I just can't seem to figure out how to get DomCrawler to find this table or if I should be using a different approach. For example,
use Goutte\Client;
$client = new Client();
$baseURL = 'http://finance.yahoo.com/';
$urlEndpoint = 'q/pr?s=MSFT+Profile';
$domSelector = '//*[@id="yfncsumtab"]/tbody/tr[2]/td[1]/table[2]/tbody/tr/td/table/tbody';
$crawler = $client->request('GET', $baseURL . $urlEndpoint);
$message = $crawler->filterXPath($domSelector)->text();
dd($message);
Any pointers are appreciated!
Upvotes: 3
Views: 2698
Reputation: 1005
I don't know something about goutte
, but you shold do the next: get response body, and pass it to crawler.
...
$html = $response->getBody(); // or $response->getContent() - it depends on tool what you are using
$crawler = new Crawler();
$crawler->addHtmlContent($html);
// use $crawler->filter() or $crawler->filterXPath()
Update:
So use filter query without tbody
, because this tag automatically creates in Google Chrome inspector and really not exists, for example (you may check - just open raw html code of the page [Ctrl+U]).
//*[@id="yfncsumtab"]/tbody/tr[2]/td[1]/table[2]/
tbody/tr/td/table/tbody
$crawler->filterXPath('//*[@id="yfncsumtab"]/tr[2]/td[1]/table[2]/tr/td/table')->text();
And you will get what you want:
string(101) "Index Membership:N/ASector:TechnologyIndustry:Business Software & ServicesFull Time Employees:118,000"
Upvotes: 2