Автор оригинала: David Wong.
Просто ознакомьтесь с информацией о провинциях и муниципалитетах Национального статистического бюро 2018 года
Примечание: Необходимо улучшить обработку исключений кода. Не распыляйте, если вам это не нравится. Благодарю
header("Content-Type: text/html;charset=UTF-8");
// Timeout settings
ini_set('max_execution_time', '0');
// Crawl Address
$url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/';
$data = curlGet($url);
$data = iconv("GBK", "UTF-8//IGNORE",$data);
preg_match_all('/provincetr\'>(.*?)<\/tr>/', $data, $matches);
if (empty($matches)) {
Return'matching exception';
}
$data2show = returnArr($matches[1]);
// Get Provincial Information
foreach ($data2show as $key => $val) {
preg_match('/=\'(\d{2}).html/', $val, $sz);
preg_match('/\'>(.{1,30})
$val) {
// Collage City Information Request Address
$cityUrl = $url.$key.'.html';
$data = curlGet($cityUrl);
$data = iconv("GBK", "UTF-8//IGNORE",$data);
preg_match_all('/citytr\'>(.*?)<\/tr>/', $data, $matches);
foreach($matches[1] as $k => $v){
// echo $v;exit;
preg_match_all('/=\'(\d{2})\/(\d{4}).html\'>(.*?)<\/a>/', $v, $info);
$city[$key][$k]['province_code'] = $info[1][1];
$city[$key][$k]['province_name'] = $val;
$city[$key][$k]['city_code'] = $info[2][1];
$city [$key][$k]['city_name']= ($info [3] [1]==='municipal district')? $val: $info [3] [1];
// print_r($city);exit;
}
// $cityData[$key] = $matches[1];
}
$cityArr = arr2ToArr1($city);
export_csv($cityArr);exit;
// curl get request
function curlGet($url)
{
$curl = curl_init();
// Setting the URL to grab
curl_setopt($curl, CURLOPT_URL, $url);
// Setting header file information as data stream output
// curl_setopt($curl, CURLOPT_HEADER, 1);
// Settings retrieve information in the form of file streams, rather than direct output.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execution of orders
$data = curl_exec($curl);
// Close URL requests
curl_close($curl);
// Display the acquired data
return $data;
}
// Input content, return array
function returnArr($content)
{
foreach ($content as $key => $val) {
$arr[$key] = explode('', '<' . trim($val, ' '));
}
$data2show = arr2ToArr1($arr);
return $data2show;
}
// Converting a two-dimensional array into a one-dimensional array
function arr2ToArr1($arr)
{
return array_reduce($arr, 'array_merge', array());
}
// Data export to CSV
function export_csv($data) {
$path = $_SERVER['DOCUMENT_ROOT']."/csv/".date("Y-m-d",time())."/";
If (! Is_dir ($path) {/// Create a directory if it exists or does not exist
mkdir($path,0777,true);
}
Filename = $path. time ().'. csv'; // Set filename
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$filename\"" );
header("Pragma: no-cache");
header("Expires: 0");
$fp= fopen($filename, 'w');
// The following line of code can be added to solve the problem of opening unscrambled code with WPS and editor but with excel.
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
} Оригинал: “https://developpaper.com/simply-grab-the-provincial-and-municipal-information-of-the-2018-national-statistical-bureau-and-export-it-to-the-csv-file-php/”