php根据接口获取天气
从网上找到一个大众的常用的天气接口:
上海:“ http://www.weather.com.cn/adat/cityinfo/101020100.html”
返回json子串:
{"weatherinfo":{"city":"上海","city_en":"shanghai","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101020100","temp1":"11C~6C","temp2":"10C~5C","temp3":"8C~4C","temp4":"8C~6C","temp5":"8C~6C","temp6":"10C~5C","tempF1":"51.8F~42.8F","tempF2":"50F~41F","tempF3":"46.4F~39.2F","tempF4":"46.4F~42.8F","tempF5":"46.4F~42.8F","tempF6":"50F~41F","weather1":"小雨转多云","weather2":"多云转阴","weather3":"小雨","weather4":"小雨","weather5":"小雨转阴","weather6":"多云","img1":"7","img2":"1","img3":"1","img4":"2","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"2","img11":"1","img12":"99","img_single":"7","img_title1":"小雨","img_title2":"多云","img_title3":"多云","img_title4":"阴","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阴","img_title11":"多云","img_title12":"多云","img_title_single":"小雨","wind1":"东北风3-4级转北风4-5级","wind2":"北风4-5级转东北风3-4级","wind3":"东风4-5级","wind4":"东风转东北风4-5级","wind5":"东北风3-4级","wind6":"东北风转东风3-4级","fx1":"东北风","fx2":"北风","fl1":"3-4级转4-5级","fl2":"4-5级转3-4级","fl3":"4-5级","fl4":"4-5级","fl5":"3-4级","fl6":"3-4级","index":"较冷","index_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"较冷","index48_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"10","st2":"3","st3":"9","st4":"2","st5":"7","st6":"2","index_cl":"不宜","index_ls":"不宜","index_ag":"易发"}}
内容格式倒是正确,信息也比较全, 可是没有指定日期的方式获取。
问题:
① 如何获取上海 当天和7天内的天气信息?
或者
② 能用正则将中国天气网的信息匹配出来?
url: http://www.weather.com.cn/weather/101020100.shtml
<div class="m m3" id="7d"><ul class="t clearfix"><li class='dn on' data-dn='7d1'><h1 id="今天">今天</h1><h2 id="日">3日</h2> <!-- 正则获取标签内容 --><big class="jpg50 d07"></big> <!-- 正则获取class内容 --><big class="jpg50 n07"></big> <!-- 正则获取class内容 --><p class="wea">小雨</p><!-- 正则获取标签内容 --><p class="tem tem1"><span>16</span><i>C</i><!-- 正则获取标签内容 --></p><p class="tem tem2"><span>13</span><i>C</i><!-- 正则获取标签内容 --></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东风" class="E"></span></em><i>微风</i><!-- 正则获取标签内容 --></p><div class="slid"></div></li><!-- 以下正则匹配规则同上 --><li class='dn' data-dn='7d2'><h1 id="明天">明天</h1><h2 id="日">4日</h2><big class="jpg50 d07"></big><big class="jpg50 n07"></big><p class="wea">小雨</p><p class="tem tem1"><span>23</span><i>C</i></p><p class="tem tem2"><span>15</span><i>C</i></p><p class="win"><em><span title="东南风" class="SE"></span><span title="北风" class="N"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d3'><h1 id="后天">后天</h1><h2 id="日">5日</h2><big class="jpg50 d04"></big><big class="jpg50 n04"></big><p class="wea">雷阵雨</p><p class="tem tem1"><span>21</span><i>C</i></p><p class="tem tem2"><span>13</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d4'><h1 id="周一">周一</h1><h2 id="日">6日</h2><big class="jpg50 d08"></big><big class="jpg50 n07"></big><p class="wea">中雨转小雨</p><p class="tem tem1"><span>15</span><i>C</i></p><p class="tem tem2"><span>10</span><i>C</i></p><p class="win"><em><span title="北风" class="N"></span><span title="北风" class="N"></span></em><i>3-4级</i></p><div class="slid"></div></li><li class='dn' data-dn='7d5'><h1 id="周二">周二</h1><h2 id="日">7日</h2><big class="jpg50 d08"></big><big class="jpg50 n08"></big><p class="wea">中雨</p><p class="tem tem1"><span>13</span><i>C</i></p><p class="tem tem2"><span>8</span><i>C</i></p><p class="win"><em><span title="北风" class="N"></span><span title="北风" class="N"></span></em><i>3-4级</i></p><div class="slid"></div></li><li class='dn' data-dn='7d6'><h1 id="周三">周三</h1><h2 id="日">8日</h2><big class="jpg50 d08"></big><big class="jpg50 n07"></big><p class="wea">中雨转小雨</p><p class="tem tem1"><span>13</span><i>C</i></p><p class="tem tem2"><span>9</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d7'><h1 id="周四">周四</h1><h2 id="日">9日</h2><big class="jpg50 d02"></big><big class="jpg50 n02"></big><p class="wea">阴</p><p class="tem tem1"><span>14</span><i>C</i></p><p class="tem tem2"><span>9</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li></ul><i class="line1"></i></div> <!-- 嵌套7天逐12小时精细化预报模块 开始--> </div>
将我注释的的内容匹配出来就行,不过这个方法麻烦了点,效率也不高。
求助。。。
回复讨论(解决方案)
找到了一个实时的接口:
http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101020100

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
