PHP uses the Simple_html_dom class to get page content and act as a crawler

Source: Internet
Author: User
Tags php script vars

PHP script to play the role of reptiles, may be the first time you think it might be a regular, personal rules of the regular can not remember, it is difficult to start, today there is a need to crawl a site some store information

Inadvertently on the Internet to see a better class library called: Simple_html_dom

Github:https://github.com/samacs/simple_html_dom

The most important step: you need to understand the structure of other people's website, and know which tab to start is the data you want

Below is a demonstration of the process.

The implementation process I took three steps.

1, the store information latitude and longitude, name and other important information first inserted into the local surface

[PHP]View PlainCopy
  1. Set_time_limit (0);
  2. $host = ' 127.0.0.1 ';
  3. $user = ' root ';
  4. $user _pwd = ";
  5. $database = ' Dataname ';
  6. $conn = mysql_connect ($host,$user,$user _pwd) or Die (' SSS ');
  7. mysql_select_db ($database,$conn) or Die (' dddd ');
  8. mysql_query (' Set names UTF8 ');
  9. Include ('./simple_html_dom-master/simple_html_dom.php ');
  10. $url = ' site URL to crawl ';
  11. $html = file_get_html ($url);
  12. $n = 1;
  13. foreach ($html->find (' li[data-counts=0] ') as $e) {
  14. $storeid = $e->storeid;
  15. $star = $e->level.  0 ';
  16. $work _time = $e->time;
  17. $mapx = $e->mapx;
  18. $mapy = $e->mapy;
  19. $nickname = $e->mapname;
  20. $mapadd = $e->mapadd;
  21. $maptel = $e->maptel;
  22. $time = date (' y-m-d h:i:s ');
  23. $query = "INSERT into ' store ' (' StoreID ', ' star ', ' Work_time ', ' longitude ', ' latitude ', ' create_time ', ' nickname ', ' Address ', ' tel ')
  24. VALUES ($storeid,' ". $star." ',' ". $work _time." ', ' ".$mapx." ', ' ". $mapy." ', ' ". $time." ', ' " ". $nickname." ',' ". $mapadd." ',' ". $maptel." ') ";
  25. $res = mysql_query ($query);
  26. //echo $query; exit ();
  27. if ($res) {
  28. echo ' successfully imported into the section '.   $n.' stores <br> ';
  29. $n + +;
  30. }else{
  31. Die (' failure <br> ');
  32. }
  33. }

2, jump into the site of another page to get store logo image

[PHP]View PlainCopy
  1. $query = "Select StoreID from store ORDER by id DESC";
  2. $row = mysql_query ($query);
  3. while ($rows = mysql_fetch_array ($row)) {
  4. $url = ' http://Others ' site domain name/'. $rows [' StoreID '].  Jhtml ';
  5. $html = file_get_html ($url);
  6. foreach ($html->find (' div.onlyonepic ') as $e) {
  7. //Get the SRC attribute of img
  8. $img = $e->firstchild ()->src;
  9. //Save remote picture to local
  10. $content = file_get_contents ($img);
  11. file_put_contents ('./store/'). $rows [' StoreID '].  JPEG ', $content);
  12. }
  13. }

3, update the table of the corresponding Store logo field

[PHP]View PlainCopy
  1. $query = "Select StoreID from store ORDER by id DESC";
  2. $row = mysql_query ($query);
  3. $n = 1;
  4. while ($rows = mysql_fetch_array ($row)) {
  5. $img = "https://my own site domain name/". $rows [' StoreID '].".  JPEG ";
  6. $sql = "UPDATE store set Img_url= '". $img. "' Where storeid=."  $rows [' StoreID '];
  7. $res = mysql_query ($sql);
  8. if ($res) {
  9. echo ' successfully updated section '.   $n.' stores <br> ';
  10. $n + +;
  11. }else{
  12. echo ' failure ';
  13. }
  14. }


OK, the function is realized, but there is no more in-depth understanding of the other functions of this class library, here is just a record, convenient for later when needed to use

PHP uses the Simple_html_dom class to get page content and act as a crawler

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.