Home >Backend Development >PHP Tutorial >Web page capture information (php regular expression, php operation excel)_PHP tutorial

Web page capture information (php regular expression, php operation excel)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:51:06934browse

Web page capture information (php regular expression, php operation excel)

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(&#39;Classes/PHPExcel.php&#39;);
require_once(&#39;Classes/PHPExcel/Writer/Excel2007.php&#39;);
$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(&#39;A&#39;.$row, &#39;rank&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;B&#39;.$row, &#39;team&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;C&#39;.$row, &#39;solved&#39;);
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(&#39;A&#39;.$row, $match[2]);
		$objPHPExcel->getActiveSheet()->getStyle(&#39;A&#39;.$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(&#39;B&#39;.$row, $match[2].$match[4]);
	}
	if(preg_match("/$problempatt/",$line,$match))
	{
		//print_r	($match);
		//echo	$match[2]." ";
		//echo	"
";
		$objPHPExcel->getActiveSheet()->setCellValue(&#39;C&#39; . $row, $match[2]);
		$objPHPExcel->getActiveSheet()->getStyle(&#39;C&#39;.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	}
	$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
	$objWriter->save(str_replace(&#39;.php&#39;, &#39;.xlsx&#39;, __FILE__));
}
echo	"well done:)";
?></a[[:space:]]></a[[:space:]]>


5. Running results

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1015087.htmlTechArticleWeb page crawling information (php regular expression, php operation excel) 1. Problem description to implement the fixed web page itself The required information is captured and stored in table form. I took a row from wustoj...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn