我收到错误:
捕获致命错误:传递给DOMXPath :: query()的参数2必须是DOMNode的实例
我是否错误地查询刮痕?该错误适用于get name of the item
和get every item image
行。在此之前,我已经从json文件创建了一个数组。此代码的目的是动态地从站点中抓取数据,并使用从json文件设置的变体。
//loop through all the items (LR)
foreach($json_array as $iscrape){
// Site Source (LR)
$iscrapeurl="The URL is here (this is removed for stackoverflow)" . $iscrape['name'];
$iscrapesource = file_get_contents($iscrapeurl);
// Create DOM document (LR)
$iscrape_doc = new DOMDocument();
// Disable libxml errors (LR)
libxml_use_internal_errors(TRUE);
//if any html is returned (LR)
if(!empty($iscrapesource)){
// Load HTML that was returned (LR)
$iscrape_doc->loadHTML($iscrapesource);
// Remove errors (LR)
libxml_clear_errors();
// allows us to do some queries with the DOM Document (LR)
$iscrape_xpath = new DOMXPath($iscrape_doc);
// Select every list item (LR)
$details = $iscrape_xpath->query('//div[@id="profile"]');
// If there are list items (LR)
if($details->length > 0){
// Get the name of the item (LR)
$name = $iscrape_xpath->query('//div[@class="heading"]', $iscrape)->item(0)->nodeValue;
// Get every item image (LR)
$image = $iscrape_xpath->query('//div[@class="profile_section_content"]/img', $iscrape)->item(0)->getAttribute('src');
答案 0 :(得分:0)
好的,我已经修好了。
虽然我应该回复我自己的问题,以防其他人试图从其他网站上删除数据。
对于每个数据都需要每个循环的A(当你想到它时这是有意义的) - 我添加了注释的行,循环遍历所有项目'并将$ iscrape更改为$ i。
// Select every list item (LR)
$details = $iscrape_xpath->query('//div[@id="profile"]');
// If there are list items (LR)
if($details->length > 0){
//loop through all the items (LR)
foreach($details as $i){
// Get the name of the item (LR)
$name = $iscrape_xpath->query('//div[@class="heading"]', $i)->item(0)->nodeValue;