user2267099
user2267099

Reputation: 45

how to scrape custom html5 attributes

I was scraping a site like flipkart.com. The problem is I am not able to scrape custom html5 attr. like data-src. Here is what I have done so far.

<?php
$link = "http://www.flipkart.com/mens-clothing/lee~brand/pr?sid=2oq%2Cs9b";
$html = file_get_html($link);
if($html && is_object($html) && isset($html->nodes))
{

    foreach ($html->find("div.pu-visual-section a img") as $el) {

 echo $a2[] = $el->data-src;

}
}
?>

For data-src (custom attr.) , it is returning 0, but if I change it to src, it is working fine. Can anybody help me to expain why I am unable to scrape data-src.

Upvotes: 1

Views: 264

Answers (2)

Denis V
Denis V

Reputation: 3370

Note this line:

echo $a2[] = $el->data-src;

What you are literally doing here is $el->data - src, i.e. subtracting src from $el->data. src is treated as a constant (as it doesn't exist it's converted to string 'src' with a notice issued) and $el just doesn't have data field and thus converted to null (with a notice issued). Both arguments are converted to integers and thus you subtract 0 from 0 resulting 0.

Fix it by:

echo $a2[] = $el->{"data-src"};

Upvotes: 2

QuarK
QuarK

Reputation: 2536

You would probably have to use it as an array:

 echo $a2[] = $el->data-src;  ->  echo $a2[] = $el['data-src'];

because you are doing an arithmetic operation there..

Upvotes: 1

Related Questions