Web Crawl information (php Normal, PHP operation Excel)

Source: Internet
Author: User

1. Description of the problem

To capture the information you need on a fixed web page and store it in a tabular format.

I took a list of wustoj to practice, address: Wustoj


2. Ideas

The Web page itself simply learn PHP, just use it to do something, my idea is this:

(1) View the source code of the webpage and save it in the file.

(2) Write the form according to the required information. Read the file and extract the required information according to the regular form. The best way to write a regular form is to group it so that it is easy to pick up.

(3) operation on Excel. The extracted information is exported as Excel.

Good open source PHP processing Excel Class Link: Click to open the link


3. Experience

^ refers to the beginning of the original string. $ refers to the end of the original string.
A null character is not necessarily a space.
Grouping with () is a good method, such as Preg_macth_all (/$pattern/, $subject, matches).


Matches is a two-dimensional array, assuming there is no _all. It will only match the first part, which is a one-dimensional array.
$matches [0] saves all matches for full mode. $matches [1] saves all matches of the first subgroup, that is, the first part of all matches.
Chinese match string I used this $PATT_CH=CHR (0x80). " -". Chr (0xFF).


4. Code

<?phpheader ("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>1<td$rankpatt=" (<td>) ([0-9]+|\*] (&LT;TD) "; Part2//<a href=status.php?user_id=team30&cid=1014>team30_ name </a> $namepatt = "(<a[[:space:]) Href=status\.php\?

user_id=team[0-9]+&cid=1014>) (\*{0,1}team[0-9]+) (_) ([$patt _ch]+) (<\/a>) "; Part2 part4//$namepatt = "(team[0-9]+) (_) ([$patt _ch]+)"; can also use this to directly match "TEAM_ name"//<a href=status.php?user_id=team30&cid=1014&jresult=4>7</a> $problempatt = "(<a[[:space:]]href=status\.php\?user_id=team[0-9]+&cid=1014&jresult=4>) ([0-9]+) (<\/a>)" ;//include classrequire_once (' classes/phpexcel.php '); require_once (' classes/phpexcel/writer/excel2007.php '); $ Objphpexcel = new Phpexcel ();//set Properties Settings File Property $objphpexcel->getproperties ()->setcreator ("Maarten Balliauw "), $objPHPExcel->getproperties ()->setlastmodifiedby (" Maarten Balliauw "); $objPHPExcel GetProperties ()->settitle ("Office" XLSX Test Document "), $objPHPExcel->getproperties ()->setsubject (" Office "XLSX Test Document"); $objPHPExcel->getproperties ()->setdescription ("Test Document for Office 2007 XLSX, generated using PHP classes. "); $objPHPExcel->getproperties ()->setkeywordS ("Office 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 '); Feof ($file)) {//echo $row. " "; $line =fgets ($file), if (Preg_match ("/$rankpatt/", $line, $match)) {$row ++;//print_r ($match);//echo$match[2]." ";//echo" <br> "; $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" <br> "; $objPHPExcel->getactivesheet ()->setcellvalue (' B '. $row, $match [2]. $match [4]);} if (Preg_match ("/$problempatt/", $line, $match)) {//print_r ($match);//echo$match[2]. " ";//echo" <br> "; $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:)";? >



5. Implementation results




Web Crawl information (php Normal, PHP operation Excel)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.