Php file_get_contents function to easily collect html data

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
// The country determines if $ REQUEST_URI contains html
If (! Strpos ($ _ SERVER ["REQUEST_URI"], ". html "))
{
$ Page = "http://qq.ip138.com/weather ";
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/<B> online query of weather trend forecasts for major cities, counties, and the next five days <\/B> (.*?) <Center style = \ "padding \: 3px \">/si ";
// Html between regular expression matching
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Replace the remote address with the local address
$ P = preg_replace ('// weather \/(\ w +) \/index.htm/', 'tq. php/$1.html ', $ pg [1]);
Echo $ p;
}
// Save. Check whether the condition is $ REQUEST_URI?
Else if (! Strpos ($ _ SERVER ["REQUEST_URI"], "? ")){
// Use the separation method recommended by yoyo to obtain data. Here, the province name is obtained.
$ Province = explode ("/", $ _ SERVER ["REQUEST_URI"]);
$ Province = explode (".", $ province [count ($ province)-1]);
$ Province = $ province [0];
// The comment is the regular expression written by myself. It is not easy to write, but the effect is equivalent to the above.
// Preg_match ('/[^ \/] + [\. (html)] $/', $ _ SERVER ["REQUEST_URI"], $ pro );
// $ Province = preg_replace ('/\. html/', '', $ pro [0]);
$ Page = "http://qq.ip138.com/weather/". $ province. "/index.htm ";
// Try to open the page before obtaining html data to prevent error caused by malicious address input
If (! @ Fopen ($ page, "r ")){
Die ("sorry, this address does not exist! <A href = javascript: history. back (1)> click here to return </a> ");
Exit (0 );
}
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/five-day weather trend forecast <\/B> (.*?) Enter city/si ";
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Obtain the province and city for regular expression replacement.
$ P = preg_replace ('// weather \/(\ w +) \/(\ ww.20..htm/', '%2.html? Pro = $ 1', $ pg [1]);
Echo $ p;
}
Else {
// City, pass the province through get
$ Pro = $ _ REQUEST ['pro'];
$ City = explode ("/", $ _ SERVER ["REQUEST_URI"]);
$ City = explode (".", $ city [count ($ city)-1]);
$ City = $ city [0];
// Preg_match ('/[^ \/] + [\. (html)] + [\?] /', $ _ SERVER ["REQUEST_URI"], $ cit );
// $ City = preg_replace ('/\. html \? /', '', $ Cit [0]);
$ Page = "http://qq.ip138.com/weather/". $ pro. "/". $ city. ". htm ";
If (! @ Fopen ($ page, "r ")){
Die ("sorry, this address does not exist! <A href = javascript: history. back (1)> click here to return </a> ");
Exit (0 );
}
$ Html = file_get_contents ($ page, 'R ');
$ Pattern = "/five-day weather trend forecast <\/B> (.*?) Enter city/si ";
Preg_match ($ pattern, $ html, $ pg );
Echo "";
// Obtain the real image address
$ P = preg_replace ('/\/image \ //', 'HTTP: // qq.ip138.com/image/', $ pg [1]);
Echo $ p;
}
?>

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.