Home >Backend Development >PHP Tutorial >Web page capture information (php regular expression, php operation excel)_PHP tutorial
1.Problem description
Capture the information you need on a fixed web page and store it in table form. I used a ranking list on wustoj to practice, address: wustoj
2. Ideas
I just learned PHP briefly on my website and just used it to do something. My idea is this:
(1) View the source code of the web page and save it in the file.
(2) Write a regular expression based on the required information, read the file, and extract the required information based on the regular expression. When writing regular expressions, it is best to group them, so that extraction is much easier.
(3) For excel operations, output the extracted information in excel form.
Better open source php handles excel links: click to open the link
3. Experience
^ means if it is the beginning of the original string, $ means if it is the end of the original string.
Null characters are not necessarily spaces.
It is a good method to use () to group, such as preg_macth_all(/$pattern/,$subject,matches).
matches is a two-dimensional array. If there is no _all, only the first part will be matched, which is a one-dimensional array.
$matches[0] holds all matches of the complete pattern. $matches[1] saves all matches in the first subgroup, that is, the first part of all matches.
The Chinese matching string I use is $patt_ch=chr(0x80)."-".chr(0xff).
4. Code
<!--?php header("Content-Type: text/html; charset=utf-8"); $url = "http://acm.wust.edu.cn/contestrank.php?cid=1014"; $result=file_get_contents($url); $file=fopen("content.php","w"); fwrite($file,$result); $file=fopen("content.php","r"); $patt_ch=chr(0x80)."-".chr(0xff); // <td-->1team30_姓名 $namepatt="()(\*{0,1}team[0-9]+)(_)([$patt_ch]+)(<\/a>)"; // part2 part4 //$namepatt="(team[0-9]+)(_)([$patt_ch]+)"; 也可以用这个直接匹配"team_姓名" //7 $problempatt="()([0-9]+)(<\/a>)"; //Include class require_once('Classes/PHPExcel.php'); require_once('Classes/PHPExcel/Writer/Excel2007.php'); $objPHPExcel = new PHPExcel(); //Set properties 设置文件属性 $objPHPExcel->getProperties()->setCreator("Maarten Balliauw"); $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw"); $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."); $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); $objPHPExcel->getProperties()->setCategory("Test result file"); $row=1; $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, 'rank'); $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, 'team'); $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, 'solved'); while(!feof($file)) { //echo $row." "; $line=fgets($file); if(preg_match("/$rankpatt/",$line,$match)) { $row++; //print_r ($match); //echo $match[2]." "; //echo " "; $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $match[2]); $objPHPExcel->getActiveSheet()->getStyle('A'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); } if(preg_match("/$namepatt/",$line,$match)) { //print_r ($match); //echo $match[2]." ".$match[4]." "; //echo " "; $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $match[2].$match[4]); } if(preg_match("/$problempatt/",$line,$match)) { //print_r ($match); //echo $match[2]." "; //echo " "; $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $match[2]); $objPHPExcel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); } $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $objWriter->save(str_replace('.php', '.xlsx', __FILE__)); } echo "well done:)"; ?></a[[:space:]]></a[[:space:]]>
5. Running results