search
HomeBackend DevelopmentPHP TutorialWeb page capture information (php regular expression, php operation excel)_PHP tutorial

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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft