0

I am new to web scraping and I am trying to scrape a few URL's at once, I have created an array with all of the URL's and I am using a for loop to get each one.

$urls = [
        "https://escapefromtarkov.gamepedia.com/Weapons",
        "https://escapefromtarkov.gamepedia.com/Headwear",
        "https://escapefromtarkov.gamepedia.com/Face_cover",
        "https://escapefromtarkov.gamepedia.com/Eyewear",
        "https://escapefromtarkov.gamepedia.com/Earpieces",
        "https://escapefromtarkov.gamepedia.com/Chest_rigs",
        "https://escapefromtarkov.gamepedia.com/Body_armor",
        "https://escapefromtarkov.gamepedia.com/Backpacks",
        "https://escapefromtarkov.gamepedia.com/Pouches",
        "https://escapefromtarkov.gamepedia.com/Armbands",
        "https://escapefromtarkov.gamepedia.com/Ammunition",
        "https://escapefromtarkov.gamepedia.com/Weapon_mods",
        "https://escapefromtarkov.gamepedia.com/Meds",
        "https://escapefromtarkov.gamepedia.com/Consumables",
        "https://escapefromtarkov.gamepedia.com/Loot",
        "https://escapefromtarkov.gamepedia.com/Keys_%26_Intel",
        "https://escapefromtarkov.gamepedia.com/Containers"
    ];


for($i = 0; $i < count($urls); $i++)
{
    $html = file_get_contents($urls[$i]);

    $wiki_doc = new DOMDocument();

    libxml_use_internal_errors(TRUE);

    $wiki_doc->loadHTML($html);
    libxml_clear_errors();

    $wiki_xpath = new DOMXPath($wiki_doc);
    $wiki_row = $wiki_xpath->query('//table[@class="wikitable"]/tbody/tr/td/a/img/@src');

    foreach($wiki_row as $row)
    {
        $row->nodeValue;
    }
}

I am looking to get the image src's of each of images within tables with a class of 'wikitable', however when I run this I get no results.

2 Answers 2

1

The tbody element is added by the browser. The developer tools DOM view shows a a cleaned up/repaired/unified HTML DOM of the page. Look at the actual source.

<table class="wikitable sortable">
<tr>
<th>Name
</th>
<th>Image
</th>
<th>Cartridge
</th>
<th>Description
</th></tr>
<tr>
<td><a href="/AK-101" class="mw-redirect" title="AK-101">AK-101</a>
</td>
<td><a href="/AK-101" title="AK-101"><img alt="AK101 Image.png" src="https://d1u5p3...

Here is no tbody and the class does not contain just wikitable. That can be matched in Xpath 1.0 but it needs a little string magic:

//table[contains(concat(' ', normalize-space(@class), ' '), ' wikitable ')]/tr/td/a/img/@src
1
  • Hi ThW, I've tried your answer and I'm still not getting anything, am I doing something else wrong? Thanks
    – Aaranihlus
    Aug 9, 2018 at 19:04
0

There are a couple of problems with the XPath, the first is that using @class="wikitable" meant that if there are other classes in the element, this won't work. You should instead say if the class contains the class your after. The second is that there isn't a <tbody> element in the original document. So the XPath line should be

$wiki_row = $wiki_xpath->query('//table[contains(@class,"wikitable")]/tr/td/a/img/@src');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.