Get PHP DOM attribute value

Asked

Viewed 903 times

1

I’m trying to make an application to take certain values from a page and insert them into a page of mine. I don’t know how to program, but I’m still taking a risk, because it’s to improve my function (bidding analyst). So far I’ve managed to get one of the data, but the other two I’m not getting. I’m using CURL to retrieve session data, and Dom to get the values I need. Follow my code so far:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);


// iniciar sessao
$ch = curl_init();
// opcoes
curl_setopt($ch, CURLOPT_URL, "https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, ');

//executar sessao
$output = curl_exec($ch);
    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }
//fechar sessao
curl_close($ch);

$dom = new DOMDocument;
$dom->loadHTML($output);
// Consultando atributo onclick
$codsprg = $dom->getElementsByTagName('a');
foreach ($codsprg as $codprg) {
$onclick[] = $codprg->getAttribute('onclick');
}
$origens  = array('Motivo_Suspensao(', ');', 'Dados_Pregao(');
$destinos = array('', '', '');
$nova = str_replace($origens, $destinos, $onclick);
$nova = array_filter($nova);
$nova = array_unique($nova);
$nova = array_slice($nova, 0);

// resultado
echo '<pre>';
//print_r ($nova);  Até aqui funciona

$numsprg = $dom->getElementsByTagName('a');
foreach ($numsprg as $numprg) {
$num[] = $numprg->getAttribute('onclick');
}
$new = $num;
echo $new;
?>

Code I want to pick up: I need to get the data of the trading floor, which is between parentheses (I already got), the number of the trading floor that is in the same row of the trading floor data(ex: 512016) and the uasg that is on the line below(ex: 155023). inserir a descrição da imagem aqui

In the first part of the code I connect to the CURL, then use the DOM to get the first data I need (data). I have tried several possible combinations with the DOM and always return errors such as:

Trying to get property of non-object e 
Call to undefined method DOMDocument::find() in

I have already researched here at Sopt and tested all possible solutions, without success. The current error is:

Notice:  Array to string conversion in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\aclicita\teste.php on line 49

The line 49 is:

echo $new;
  • 1

    It would be better to say more specifically which errors and which lines. About "I don’t know how to program, but I’m still taking a risk", it’s not a problem, because most of the things with PHP here fit perfectly in this profile (even if the author "swears he’s a programmer"). If you can [Dit] the issue and split your problem into smaller problems, or detail the bugs, it helps. A good question regardless of your current ability to program. Here are some tips: [Ask], [Help].

  • @Inkeliz, the data I need are in the attached image. Dados_pregao(XXXXXX) = I need to get the numbers between parentheses (So far I got. On the same line has the return that is 512016 (this data I need). And on the line just below has 155023 (this is the other data I need). The rest I don’t need.

  • @Bacco, I’ll report the error as is the code now.

  • So that the clasps in a?

  • I put for a better view with print_r, to number the output so I can use later, like: [0], [1], ... If I’m wrong correct me, please.

  • in this case do not use echo to show, use print_r

  • My fault, thank you. But let’s see, follow the image with this code I can get the data.

  • Good evening, I manually downloaded the CURL "https://www.comprasnet.gov.br/pregao/provid/Acompanhar.asp" URL and show a denied access message redirecting to https://www.comprasnet.gov.br/accessdenied.htm

  • This page is only accessible after logging in to the browser.

  • Still using Curl, first you need to log in!

Show 5 more comments

1 answer

0

Well, after a while taking a look at the gift and its functions, I managed to make my code work. Below is code that is working.

<?php
header("Content-type: text/html; charset=utf-8");
error_reporting( error_reporting() & ~E_NOTICE );
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

//Converte caracteres acentuados para não acentuado
$conversao = array('á' => 'a','à' => 'a','ã' => 'a','â' => 'a', 'é' => 'e', 'ê' => 'e', 'í' => 'i', 'ï'=>'i', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', "ö"=>"o", 'ú' => 'u', 'ü' => 'u', 'ç' => 'c', 'ñ'=>'n', 'Á' => 'A', 'À' => 'A', 'Ã' => 'A', 'Â' => 'A', 'É' => 'E', 'Ê' => 'E', 'Í' => 'I', 'Ï'=>'I', "Ö"=>"O", 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ú' => 'U', 'Ü' => 'U', 'Ç' =>'C', 'Ñ'=>'N');

// iniciar sessao
$ch = curl_init();
// opcoes
curl_setopt($ch, CURLOPT_URL, "https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, 'ASPSESSIONIDSCRRTCSB=LPDMGOACKEJMOBEAJNBNELNC; ASPSESSIONIDQCQRRBQC=ENJMGOACPDMCKBMILFDFHMNJ');

//executar sessao
$output = curl_exec($ch);
    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }
$output = strtr($output, $conversao); // função de conversão acentuado/não acentuado
//fechar sessao
curl_close($ch);

$dom = new DOMDocument;
$dom->loadHTML($output);
// Pega os códigos de cada pregão
$codsprg = $dom->getElementsByTagName('a');
foreach ($codsprg as $codprg) {
$onclick[] = $codprg->getAttribute('onclick');
}
$origens  = array('Motivo_Suspensao(', ');', 'Dados_Pregao(');
$destinos = array('', '', '');
$nova = str_replace($origens, $destinos, $onclick);  // remove palavras indejadas
$nova = array_filter($nova);
$nova = array_unique($nova);
$nova = array_slice($nova, 0);

// resultado
//echo '<pre>';
//print_r ($nova);

// Pegar os valores do número do pregão
$numsprg = $dom->getElementsByTagName('a');
foreach ($numsprg as $item) {
$prg[] = $item->nodeValue . "\n";
$prg = array_map('trim', $prg); // remove os espaços em branco
}
$origensprg  = array('Suspensao', 'Administrativa', 'Acompanhar', ' ');
$destinosprg = array('', '', '', '');
$newprg = str_replace($origensprg, $destinosprg, $prg); // remove palavras indejadas
$newprg = array_filter($newprg);
$newprg = array_slice($newprg, 0);
// print_r ($newprg);

//Pega os valores da uasg
$numsuasg = $dom->getElementsByTagName('td');
foreach ($numsuasg as $numuasg) {
$uasg[] = $numuasg->nodeValue . "\n";
$uasg = array_map('trim', $uasg); // remove os espaços em branco
}
$newuasg = preg_replace("/[^0-9]/", "", $uasg);   // remove caracteres não numericos do output
$newuasg = array_filter($newuasg);
$newuasg = array_slice($newuasg, 2);
//print_r($newuasg);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.