Goutte icon indicating copy to clipboard operation
Goutte copied to clipboard

Get url and title with same filter

Open Takuaraa opened this issue 7 years ago • 8 comments

Hello,

Is it possible to have a "filter"-code inside another "filter"-code. For example I have this to get me the urls from a website:

...
$links = $crawler->filter('a')->each(function ($node) use ($url){
    $l = $node -> attr('href');

    ..code..

      return $l;
  });

This returns me something like: abc.com, abc.com/defg, ... Now I would like to get also the titles from those links found above. The code would be this:

$titles = $crawler->filter('title')->each(function ($node) {
     $content .= "Title: ".$node->text()."<br>";
     return $content;
   });

Is it possible to have the second part of the code (title) inside the first part(link), so that whenever it gets the link it also gets the title of that link?

I hope I was understandable. Thank you.

Takuaraa avatar Oct 16 '17 14:10 Takuaraa

$links = $crawler->filter('a')->each(function($node) {
    $href  = $node->attr('href');
    $title = $node->attr('title');
    $text  = $node->text();

    return compact('href', 'title', 'text');
});

dpde avatar Oct 16 '17 14:10 dpde

Thank you so much. I only have one more question. Is there also a way to get the description of a link?

Takuaraa avatar Oct 16 '17 14:10 Takuaraa

What do you mean by description? Can you post an example of that link?

dpde avatar Oct 16 '17 14:10 dpde

youtube.com

<meta name="description" content="Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.">

Takuaraa avatar Oct 17 '17 07:10 Takuaraa

That is not a link tag, thats a meta tag.

If that tag is on the linked page, you have to request that page and extract that information.

dpde avatar Oct 17 '17 07:10 dpde

$crawler->filterXpath('//meta[@name="description"]')->attr('content');

This line of code should give me the description but how do I use it with the code from before?

Takuaraa avatar Oct 19 '17 09:10 Takuaraa

$node->text()

$node->text() always is empty =(

kowap avatar Sep 13 '18 07:09 kowap

$description = $crawler->filterXpath("//meta[@name='description")->extract('content'); $description will contain the meta tag description.

RachidBourougaa avatar Nov 06 '18 18:11 RachidBourougaa