PHP to capture the weather in China 7 days _php Example

Source: Internet
Author: User

Objective

We are writing a Web program, always think of their own site more beautiful, more functions, and sometimes write small tools or add small plug-ins will make our site more perfect. such as the calendar function, such as we now want to talk about the weather function.

Of course we can't use professional satellites to receive data, so our weather data comes from existing weather forecast sites. Using the data services provided by the weather forecast website, we can write a PHP crawler, then dynamically collect the data we need, and when the target site updates the data, our program can also be synchronized update, automatic access to data.

Here's how to write a simple PHP Data acquisition program (PHP crawler).

Principle

Given a URL to a Web page, use PHP to download the page and get the content of the Web page, then extract the data that we are interested in by regular expressions and then output it.

Specifically in this example, we want to crawl the page is http://www.weather.com.cn/weather/101050101.shtml, we are interested in the page in the next 7 days weather conditions.

Realize

0. Get the URL of the Weather report page:

Copy Code code as follows:

$url = "http://www.weather.com.cn/weather/101050101.shtml";
$page _content = file_get_contents ($url);

Here, the file_get_contents () function downloads the Web page that the $url points to, and returns the contents of the Web page as a string. So, $page _content variable is the entire HTML code for the page we're crawling. Next, we'll extract the data we need from it.

1. Using regular expressions to match strings with criteria

First output the value of the $page _content, and then look at the source code of the Web page and see that the string we need can be

Copy Code code as follows:

<!--Day 1-->
......
<!--Day 7-->

These two lines are found in the comments.

Use regular expressions to get everything between <!--day 1--> and <!--day 7-->:

Copy Code code as follows:

Eregi ("<!--day 1--> (. *) <!--day 7-->", $page _content, $res);

2. The path of the picture in the full page

Since the image path in the Remote Web page is a relative path like/m2/i/icon_weather/29x20/d01.gif, we need to fill these paths up and precede them with http://www.weather.com.cn.

Copy Code code as follows:

$forecast = Str_replace ("

At this point, $forecast is the weather information we need. This simple PHP crawler is also ready to write.

Source

The following is the complete source code for the crawl Weather applet, which adds some code to measure the running time of each part of the program, and can control how many days of crawling information by setting the $start and $end values.

Copy Code code as follows:

$url = "http://www.weather.com.cn/weather/101050101.shtml";
$t 1 = time ();

$page _content = file_get_contents ($url);
$t 2 = time ();

$start = 1;
$end = 3;

if ($end > 7) {
echo "is out of the forecast capability, please reset it!" ";
}else {
echo "The Future". ($end-$start). " Weather forecast for Harbin ("
. Date (' Y-m-j '). " Published) ";

Eregi ("--day $start-(. *)--day $end-", $page _content, $res);

$forecast = Str_replace ("<img src=\"),
"$t 3 = time ();

Echo $forecast;

echo ' costs '. ($t 2-$t 1). ' Ms. ';
Echo ' last step costs '. ($t 3-$t 2). ' Ms. ';
}

Other application examples

The same ideas can be tried to: the NBA daily score card, today hit the news synchronization, the stock market and so on. Can realize synchronous real-time update. Temporarily only think of these, welcome everybody to shoot the brick ~

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.