Home php教程 PHP源码 抓取最近八天天气(非利用接口,直接从网站提取)

抓取最近八天天气(非利用接口,直接从网站提取)

Jun 01, 2016 pm 02:33 PM
weather crawl

php代码

<?php
   /*
    *特别注意,第一天没有最高气温数据,第八天没有最低气温数据
    *注意对数字进行过滤时不要忘记对负号进行判断
    *对风力过滤时要考虑到3-5级这种格式
    */
   class weatherfetch{
       private $f;
       function getNum($string) {
           $tmpstr = &#39;&#39;;
           $strlen = strlen($string);
           for($i=0; $i<$strlen; $i++) {
                $str=substr($string, $i, 1);
                $str1=trim($str);
                if(is_numeric($str1)){
                    $tmpstr.=$str1+0;
  
                }
                if($str1=="-"&&is_numeric(substr($string, $i-1, 1))){
                $tmpstr.= $str1;
            }
  
            }
           return $tmpstr;
        }
        function __construct(){
          $this->f= new SaeFetchurl();
  
        }
        function getChineseNum($string){
           $tmpstr = &#39;&#39;;
           $arr = array(1,2,3,4,5,6,7,8,9,0);
          $strlen = strlen($string);
          for($i=0; $i<$strlen; $i++) {      
  
           $str=substr($string, $i, 1);
  
           $str1=trim($str);
           if( ord($str)>0xA0 ){
  
            $tmpstr.= substr($string, $i, 3);
  
            $i = $i+2;
  
           }
  
            if(is_numeric($str1)){
  
                $tmpstr.= $str1;
  
            }
            if($str1=="-"&&is_numeric(substr($string, $i-1, 1))&&is_numeric(substr($string, $i+1, 1))){
                $tmpstr.= $str1;
            }
  
          }
  
                return $tmpstr;
  
        }
        function getChinese($string,$encode="GBK") {
               switch($encode){
                    case "GBK" :$codelength=2;break;
               case "GB2312" :$codelength=3;break;
               case "UTF-8" :$codelength=3;break;
                   case "UTF-16" :$codelength=4;break;
  
               }
               $tmpstr = &#39;&#39;;
               $arr = array(1,2,3,4,5,6,7,8,9,0);
               $strlen = strlen($string);
               for($i=0; $i<$strlen; $i++) {
                    $str=substr($string, $i, 1);
                    $str1=trim($str);
                    if( ord($str)>0xA0 ){
                    $tmpstr.= substr($string, $i, $codelength);
                    $i = $i+$codelength-1;
                    }
  
                }
            return $tmpstr;
        }
       function get($cityid){
           $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
           $data=$this->f->fetch($url);
  
           $sun=explode(&#39;<div class="weatherTopright">&#39;,$data);
           $sun=explode("<dl>",$sun[1]);
           $sun=explode("</dl>",$sun[1]);
           $sun=explode("</strong>",$sun[0]);
           $sunrise=strlen($sun[0]);
           $sunrise=substr($sun[0],$sunrise-5);//日出时间
           $sunset=strlen($sun[1]);
           $sunset=substr($sun[1],$sunset-5);//日落时间
           $sunhour=substr($sunset,0,2)-substr($sunrise,0,2);
           $sunminute=$sunhour*60+substr($sunset,-2)-substr($sunrise,-2);//日照时间
           $yubao=explode(&#39;class="yuBaoTable"&#39;,$data);
           $num=count($yubao);
           $tl=array();
           $th=array();
           $fx=array();
           $fl=array();
           $weather=array();
           //第一天
           $tr=explode("</tr>",$yubao[1]);
           $td=explode("</td>",$tr[0]);
           $weather[]=$this->getChinese($td[3],"UTF-8");//晚上天气
           $fx[]=$this->getChinese($td[5],"UTF-8");//晚上风向
           $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//晚上风力
           $tltemp=explode("<strong>",$td[4]);//最低气温
           $tl[]=$this->getNum($tltemp[1]);
           //从第二天到第七天
           for($i=2;$i<$num-1;$i++){
               $tr=explode("</tr>",$yubao[$i]);
               $td=explode("</td>",$tr[0]);
               $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气
               $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向
               $fltemp=substr($this->getChineseNum($td[6],"UTF-8"),5);
               $fl[]=$fltemp;//白天风力
               $thtemp=explode("<strong>",$td[4]);
               $th[]=$this->getNum($thtemp[1]);//最高气温
               $td=explode("</td>",$tr[1]);
               $tltemp=explode("<strong>",$td[3]);
               $tl[]=$this->getNum($tltemp[1]);//最低气温
  
            }
            //第八天
            $tr=explode("</tr>",$yubao[$num-1]);
            $td=explode("</td>",$tr[0]);
            $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气
            $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向
            $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//白天风力
            $thtemp=explode("<strong>",$td[4]);
            $th[]=$this->getNum($thtemp[1]);//最高气温
            if(count($weather)==8){
                return array("weather"=>$weather,"tl"=>$tl,"th"=>$th,"fx"=>$fx,"fl"=>$fl,"sunset"=>$sunset,"sunrise"=>$sunrise,"sunminute"=>$sunminute);
            }else{
              return 1;
  
            }
        }
        function getday1($cityid){
             $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
             $data=$this->f->fetch($url);
             $yubao=explode(&#39;class="yuBaoTable"&#39;,$data);
             $tr=explode("</tr>",$yubao[1]);
             $td=explode("</td>",$tr[0]);
             $thtemp=explode("<strong>",$td[4]);
             return $this->getNum($thtemp[1]);
  
        }
    }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

7 fixes to make if Windows 11's weather widget isn't showing up in the taskbar 7 fixes to make if Windows 11's weather widget isn't showing up in the taskbar Apr 16, 2023 pm 12:13 PM

Windows 11 got rid of the News and Interests section and replaced it with Widgets, which you can turn on or off through settings. Weather app comes with widgets which shows all the details and information you want about the weather. To do this, you have to click and open the app to view it. Some users experience difficulty when trying to display the weather in the taskbar. Some people end up abandoning the feature because they can't solve the problem. Many people have reported missing taskbar icons on Windows 11, but this is easy to fix. For more information about the weather widget, be sure to keep reading. What's the reason why Windows 11 weather doesn't show up on the taskbar?

How to change the language of Windows 11 widgets How to change the language of Windows 11 widgets May 12, 2023 pm 04:58 PM

Windows 11 widgets are small programs that display information on the desktop. These can display weather, stocks or news headlines. You can also use them to display pictures or videos on your desktop. By default, Windows 11 language is set to English, but users claim that the widget bar displays a different language. This puts non-English speakers at a disadvantage as they may not be able to use the widget. If you find them useless, you can easily disable the widgets and continue your activity. However, if you find that they make your life easier, the good news is that you can change the language if you want. Why are my widgets in different languages? If you find that the widget displays in a different language, here are a few reasons: Country or Region Settings – You may have

The 5 Best Weather Apps for Windows 11 [2022 List] The 5 Best Weather Apps for Windows 11 [2022 List] Apr 13, 2023 pm 07:01 PM

The Windows 11 Weather app for desktop is a great tool for people who want to be prepared for whatever weather it may bring. It's simple, easy to use and user-friendly. Weather apps are an absolute must-have when planning your day. This is because it can help you avoid getting caught in the rain and can even help you plan your wardrobe for the day. The Windows 11 Weather app does a great job, but some users may be looking for more than just a simple weather app. Others may be looking for a replacement because their Windows weather app isn't working. This led them to source resources for other weather applications. Most of them provide you with a variety of additional features

Scrapy case analysis: How to crawl company information on LinkedIn Scrapy case analysis: How to crawl company information on LinkedIn Jun 23, 2023 am 10:04 AM

Scrapy is a Python-based crawler framework that can quickly and easily obtain relevant information on the Internet. In this article, we will use a Scrapy case to analyze in detail how to crawl company information on LinkedIn. Determine the target URL First, we need to make it clear that our target is the company information on LinkedIn. Therefore, we need to find the URL of the LinkedIn company information page. Open the LinkedIn website, enter the company name in the search box, and

How to remove the weather widget on Windows 11 How to remove the weather widget on Windows 11 May 11, 2023 pm 08:25 PM

How to Remove the Weather Widget from the Taskbar in Windows 11 One of the easiest ways to get rid of the weather widget is to disable it in Settings. To disable the weather widget through settings, use the following steps: Press Start and select Settings. When settings open, click Personalize in the list on the left. On the right, select Taskbar Options. Expand the Taskbar Items section and turn off the Widgets switch. When you turn off the switch, the weather widget disappears from the taskbar. If you want to re-enable it, go back to Start > Settings > Personalization > Taskbar and turn the widgets switch back on. Note: You can turn other taskbar buttons on or off in this area of ​​the Settings menu. For example, you can remove the Teams chat icon. How to pass

Example of scraping Instagram information using PHP Example of scraping Instagram information using PHP Jun 13, 2023 pm 06:26 PM

Instagram is one of the most popular social media today, with hundreds of millions of active users. Users upload billions of pictures and videos, and this data is very valuable to many businesses and individuals. Therefore, in many cases, it is necessary to use a program to automatically scrape Instagram data. This article will introduce how to use PHP to capture Instagram data and provide implementation examples. Install the cURL extension for PHP cURL is a tool used in various

Weather widget not showing up in Windows 11 [Resolved] Weather widget not showing up in Windows 11 [Resolved] Apr 13, 2023 pm 11:10 PM

The weather widget is a feature that allows users to view weather information for their current location, which Windows automatically detects on their systems and displays on the taskbar. But recently many Windows users have reported that they cannot see the weather widget display on the taskbar. They may not have it enabled on their system, but they don't know about it, and some users think it's enabled by default in Windows. The weather widget not showing issue can also be due to a corrupted user account profile. Such issues may result if Windows users do not download and install the necessary Windows updates. If your system is also facing such issues, don’t worry, we have compiled a series of fixes in this article for

How to turn off the weather in the lower right corner of WIN10 How to turn off the weather in the lower right corner of WIN10 Feb 18, 2024 pm 01:38 PM

The function of displaying weather in the lower right corner of the WIN10 operating system is implemented by the weather application that comes with the system. If you do not want to display the weather in the lower right corner of the taskbar, you can turn it off through the following methods. Method 1: Close through the settings menu. Click the start menu and select the "Settings" icon (gear-shaped icon). In the settings window, select the "Personalize" option. In the menu on the left side of the personalization window, select Taskbar. On the right side of the taskbar settings window, find the "System Icons" area and click "Show or hide system icons on the taskbar.

See all articles