Web crawl information (PHP regular expression, PHP operation Excel) _php tutorial

Source: Internet
Author: User
Tags php regular expression

Web crawl information (PHP regular expression, PHP operation Excel)

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

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 regular expression according to the required information, read the file, and extract the required information according to the regular expression. When writing regular expressions, it is best to group them so that they are easier to extract.

(3) For Excel operation, the extracted information is exported in Excel form.

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

3. Experience

^ refers to the beginning of the original string, which means that if 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, and if there is no _all, it will only match the first part, which is a one-dimensional array.
$matches [0] saves all matches for the full pattern. $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

 1team30_ name $namepatt= "() (\*{0,1}team[0-9]+) (_) ([$patt _ch]+) (<\/a>)";   Part2 part4//$namepatt = "(team[0-9]+) (_) ([$patt _ch]+)"; You can also use this to directly match "TEAM_ name"//7$problempatt= "([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" 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 '), 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:)";? >

5. Running Results

http://www.bkjia.com/PHPjc/1015087.html www.bkjia.com true http://www.bkjia.com/PHPjc/1015087.html techarticle Web Crawl information (PHP regular expression, PHP operation Excel) 1. The problem description implements the information that is required on the fixed web page to be crawled and stored in tabular form. I'm taking a platoon on the Wustoj .

  • Related Article

    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.