unable to scrape content from a website

前端 未结 5 2276
清酒与你
清酒与你 2020-12-06 13:03

I am trying to scrap some content from a website but the code below is not working(not showing any output). here is the code

$url=\"some url\";
$otherHeader         


        
5条回答
  •  旧巷少年郎
    2020-12-06 13:47

    (Try the following both in combination with and separately from the other answers, as they are other possible caveats.)

    If your XPath isn't working, try applying just parts of it to make sure you are indeed following the right path. So do something like:

    $path1="//body";
    $item1 = $xpath->query($path1);
    
    foreach ($item1 as $t) {
        // to see the full XML of the returned node, as the nodeValue may be empty
        echo $t->ownerDocument->saveXML($t); 
    }
    

    Then keep increasing your XPath to the location you want.

    Also, if you find that nodeValue and textContent of your nodes are empty, you should make sure that you are loading into the DOMDocument with the correct encoding (e.g. if the cURL response returns UTF-8, you'll need to pass 'UTF-8' as the second parameter when constructing your DOMDOcument).

提交回复
热议问题