首页 > 其他分享 >使用fetch获取wttr.in天气预报

使用fetch获取wttr.in天气预报

时间:2023-08-29 16:37:38浏览次数:376  
标签:wttr clock res cd dackorlight var 天气预报 fetch card

使用fetch获取wttr.in天气预报

https://wttr.in/可以看到当前ip的天气预报情况,在网页上想快速取得数据并组装为想显示的样式就需要自行处理了

这里我们来尝试实现一个LED时间显示功能天气预报

使用fetch获取wttr.in天气预报_html

 

样式表clock.css

@font-face {
    font-family: 'UnidreamLED'; /*a name to be used later*/
    /*src: url('UnidreamLED.ttf'); *//*URL to font*/
    src: url('https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/fonts/UnidreamLED.ttf');
}
#hexo_electric_clock{
padding:20px
}
.card-clock{
    padding: 0!important;
}
.card-background {
    height: 153px;
    margin: 15px;
    border-radius: 8px;
    background-color: #ececec; /*背景色*/	
    box-shadow: inset 3px 3px 18px 0 rgb(50 50 50 / 40%);
    z-index: 2;
}
.clock-row{
    font-family: UnidreamLED;
    font-weight: 400;
    color: #000;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    white-space: nowrap;
}
.card-clock-clockdate,.card-clock-ip{
    text-align: left;
    flex-basis: calc(40% - 5px);
}
.card-clock-weather,.card-clock-location{
    text-align: center;
    flex-basis: calc(30% - 5px);
}
.card-clock-humidity,.card-clock-dackorlight{
    text-align: right;
    flex-basis: calc(30% - 5px);
}
.card-clock-time{
    flex: 1;
    font-family: UnidreamLED;
    font-size: 38px;
    text-align: center;
    line-height: 1.5;
}

clock.js

这个参数returnCitySN = {"cip": "", "cid": "00", "cname": "未知"};如果cip写上具体的IP也能要ip地址来获取天气预报

有些在云端的IP无法取得天气预报

var returnCitySN = {"cip": "", "cid": "00", "cname": "未知"};
   fetch('https://wttr.in/'+returnCitySN["cip"]+'?format="%l+\\+%c+\\+%t+\\+%h"').then(res=>res.text()).then(
    data => {
       if(document.getElementById('hexo_electric_clock')){
        var res_text = data.replace(/not found/g,'not found,not found').replace(/"/g,'').replace(/\+/g,'').replace(/,/g,'\\').replace(/ /g,'').replace(/°C/g,'');
        res_list = res_text.split('\\');
        var clock_box = document.getElementById('hexo_electric_clock');
        clock_box_html = `  
  <div class="clock-row">
<span id="card-clock-clockdate" class="card-clock-clockdate"></span>
<span class="card-clock-weather">${res_list[2]} ${res_list[3]} *C</span>
<span class="card-clock-humidity">

标签:wttr,clock,res,cd,dackorlight,var,天气预报,fetch,card
From: https://blog.51cto.com/u_12668715/7277565

相关文章

  • node-fetch Advanced Usage All In One
    node-fetchAdvancedUsageAllInOnefetch//streamhttps://www.npmjs.com/package/node-fetch#streamsdemosNode.jswebcrawlerimportfetchfrom"node-fetch";importpathfrom'node:path';import{fileURLToPath}from'no......
  • 实现Fetch 请求扩展超时功能
    要实现基本的超时功能其实很简单,只需要使用AbortController这个API,如果你不熟悉它,可以点击链接了解一下/***@description:创建一个fetch函数*@param{*}timeout:传入超时的时间*@return{*}返回一个新的fetch函数*/functioncreateFetch(timeout){//......
  • vue项目在360浏览器兼容模式下SCRIPT1002: 语法错误以及“fetch”未定义问题解决
    使用360浏览器的兼容模式,vue项目页面空白,打开控制台,发现如下报错:SCRIPT1002:语法错误 解决方法如下:1、安装依赖npminstall--savecore-jsregenerator-runtime2、在main.js引入import'core-js/stable';import'regenerator-runtime/runtime';3、在babel.confi......
  • ​python爬虫——爬取天气预报信息
    在本文中,我们将学习如何使用代理IP爬取天气预报信息。我们将使用Python编写程序,并使用requests和BeautifulSoup库来获取和解析HTML。此外,我们还将使用代理服务器来隐藏我们的IP地址,以避免被目标网站封禁。1.安装必须的库首先,我们需要安装必须的库,包括requests、beauti......
  • ​python爬虫——爬取天气预报信息
    在本文中,我们将学习如何使用代理IP爬取天气预报信息。我们将使用Python编写程序,并使用requests和BeautifulSoup库来获取和解析HTML。此外,我们还将使用代理服务器来隐藏我们的IP地址,以避免被目标网站封禁。1.安装必须的库首先,我们需要安装必须的库,包括requests、beauti......
  • ​python爬虫——爬取天气预报信息
    在本文中,我们将学习如何使用代理IP爬取天气预报信息。我们将使用Python编写程序,并使用requests和BeautifulSoup库来获取和解析HTML。此外,我们还将使用代理服务器来隐藏我们的IP地址,以避免被目标网站封禁。1.安装必须的库首先,我们需要安装必须的库,包括requests、beaut......
  • Linux下C语言调用libcurl库获取天气预报信息
    一、概述当前文章介绍如何在Linux(Ubuntu)下使用C语言调用libcurl库获取天气预报的方法。通过HTTPGET请求访问百度天气API,并解析返回的JSON数据,可以获取指定城市未来7天的天气预报信息。二、设计思路【1】使用libcurl库进行HTTPGET请求在代码中包含<curl/curl.h>头文件,以便使用libc......
  • fetch_assoc()和fetch_array()区别
    fetch_assoc()得到的是关联数组例:array([username]=>'test',[password]=>'123456') fetch_array()得到的有关联数组也有索引数组,根据数据库中数据来决定。例:array([username]=>'test',[password]=>'123456')array([0]=>'test',[1]=>......
  • Git fetch-pack: unexpected disconnect while reading sideband packet
    GitClone拉取代码提示以下错误remote:Enumeratingobjects:57218,done.remote:Countingobjects:100%(57218/57218),done.remote:Compressingobjects:100%(15392/15392),done•error:7107bytesofbodyarestillexpectedGiB15.03MiB/sfetch-pack:une......
  • 条件竞争漏洞Double Fetch
    前言DoubleFetch(双取)是一种条件竞争的漏洞,相关的论文发表在USENIX,论文链接:https://www.usenix.org/system/files/conference/usenixsecurity17/sec17-wang.pdfDoubleFetchDoubleFetch是内核的一种漏洞类型,发生在内核从用户空间中拷贝数据时,两次访问了相同一块内存。如下图......