M
M
Mayuko2019-09-09 08:48:02
PHP
Mayuko, 2019-09-09 08:48:02

How to properly parse with phpQuery and put everything in order?

How to correctly parse a site using phpQuery so that all data is stored in a json document as follows:
id_region:"1", "city": "Adygeysk" , "street": "Leningradskaya street" id_region:"1", "city": " Maikop" , "street": "Leningradskaya street" and so all the data for id_region 1, then after it comes let's say id_region: "2", "city": "Agidel" , "street": "Activists street" and so on . Now I understand how to parse everything in a heap, but I don’t understand how to parse everything in order, so that I have data stored in the file in the format as I described above. I need all this in order to add everything in order to the mysql database.
In this code, json is formed like this "

$url = 'https://kladr-rf.ru/';
$file = file_get_contents($url);

$id_region_files = file_get_contents($url);
$document = phpQuery::newDocument($id_region_files);
$id_region_find = $document->find('.badge badge-info')->text();

for($i = 1; $i < 5; $i++) {  
  $url_region = 'https://kladr-rf.ru/0'.$i.'/000/'; 
  $region_files = file_get_contents($url_region);
  $document = phpQuery::newDocument($region_files);
  $needle = "Город";
  $region_find = $document->find('.span4 a:contains("'.$needle.'")')->text();
  $region_next = $document->find('.span4 a:contains("'.$needle.'")')->next()->attr('href');

  $array_cities[] = $region_find;
  
  for($j = 1; $j < 5; $j++) { 
    $url = 'https://kladr-rf.ru/0'.$i.'/000/00'.$j.'/000/';
    $files = file_get_contents($url);
    $document = phpQuery::newDocument($files);
    $street_find = $document->find('.span4')->text();
    $array_streets[] = $street_find; 
    
  }

}

$result_city = array_unique($array_cities);
$result_streets = array_unique($array_streets);
print_r($result_streets);

$file = file_get_contents('data.json');
$array_kldr = json_decode($file,TRUE);
unset($file);
$array_kldr = [1
'city'=>$result_city,
'streets'=>$result_streets,
];

file_put_contents('data.json',json_encode($array_kldr));
unset($array_kldr);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-09-09
@IgorPI

In order for everything to be on the shelves.
Create an object.
For example:

$Street = new stdClass();
$Street->street_name = "Генерала Лизюкова";
$Street->street_type = "улица";

$City = new stdClass();
$City->city_name = "Воронеж"

$MyObjectItem = new stdClass();
$MyObjectItem->street = $Street;
$MyObjectItem->city = $City;

$MyObject = new stdClass();

$MyObject->count = 1;
$MyObject->items = []; // Сюда положим объекты
$MyObject->items[] = $MyObjectItem;

file_put_contents('data.json', json_encode($MyObject));

And yes, this is unset($array_kldr); not at all necessary!
In case the script does not assume further execution!
Extra cycles to free memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question