对于循环功能,虽然真正难以理解 [英] For loop in function, While true difficulty to understand

查看:84
本文介绍了对于循环功能,虽然真正难以理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个简单的Web Weather Scraping脚本放在一起,以检查给定位置的温度.该代码可以完美运行,即使它可能不是最佳或最干净的版本.仍然在学习.但这很麻烦:<span _ngcontent-c19="" class="wu-value wu-value-to">67</span>来自

I put this simple Web Weather Scraping script together, to check a temperature in a given location. The code works perfectly, even though, it may not be the best, or cleanest version. Still learning. But it is scraping: <span _ngcontent-c19="" class="wu-value wu-value-to">67</span> from HERE.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
from BeautifulSoup import BeautifulSoup
import time

degree = u'\N{DEGREE SIGN}'

url = 'https://www.wunderground.com/weather/us/ca/san-diego/KCASANDI355'

response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
current_temp = soup.find("div", {"class" : "current-temp"}).find("span", {"class" : "wu-value wu-value-to"})

for i in current_temp:
    print('San Diego Feels Like ') + i + (degree + 'F')

输出看起来像这样:

San Diego Feels Like 74°F

我的目标是要有一个循环的函数,并根据在current_temp变量中定义的当前温度进行打印,例如打印温度是否低于70F It's too cold或高于80F It is too hot等 但是,我在理解如何告诉我的代码执行(或者在这种情况下print您可以说的这些不同任务或方法)方面遇到问题.打扰一下我的英语. While (True):循环肯定存在问题,但是我无法解决.感谢您的帮助,并祝大家星期天一切顺利.

My goal here is to have a function that is looping, and based on the current temperature that is defined in current_temp variable to print for example if the temperature goes bellow 70F It's too cold, or if it goes above 80F It is too hot, etc. However I am having an issue to understand how to tell my code to execute, or in this case print these different tasks or methods you can say?. Excuse my English. There is definitely something wrong with the While (True): loop, but I can't get my head around it. Thanks for any help, and wish you all good Sunday.

#!/usr/bin/python
import requests
from BeautifulSoup import BeautifulSoup
import time

degree = u'\N{DEGREE SIGN}'

url = 'https://www.wunderground.com/weather/us/ca/san-diego/KCASANDI355'

response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
current_temp = soup.find("div", {"class" : "current-temp"}).find("span", {"class" : "wu-value wu-value-to"})

def weather():
    while(True):
        for i in current_temp:
            print('San Diego Feels Like ') + i + (degree + 'F')
            #time.sleep(2)
        if (i <= 70) and (i >= 50):
            print('It\'s kinda cool')
            break
        elif i <= 50:
            print('It\'s cold af')
        elif (i >= 80) and (i <= 100):
            print('It\'s hot af')
            break
        else:
            print('You Dead')
            break
if __name__ == "__main__":
    weather()

推荐答案

首先,即使您只对显示的值感兴趣,也要收集整个<span>标签,以便获取温度值(并将其转换为实际整数):

First of all, you're collecting the whole <span> tag even tho you're only interested in the value presented, so to get the temperature value (and convert it to an actual integer) do:

current_temp = int(soup.find("div", {"class": "current-temp"}).find(
    "span", {"class": "wu-value wu-value-to"}).getText())

第二,您的current_temp一旦获得就不会改变,您想要的是定期获取最新的温度值,然后根据其值打印所需的任何内容.像这样:

Second, your current_temp will never change once obtained, what you want instead is to periodically pick up the latest temperature value and then, based on its value, print whatever you want. Something like:

# !/usr/bin/python
import requests
from BeautifulSoup import BeautifulSoup
import time

degree = u'\N{DEGREE SIGN}'
url = 'https://www.wunderground.com/weather/us/ca/san-diego/KCASANDI355'

def weather():
    while (True):
        # get the current temperature
        response = requests.get(url)
        soup = BeautifulSoup(response.content)
        current_temp = int(soup.find("div", {"class": "current-temp"}).find(
            "span", {"class": "wu-value wu-value-to"}).getText())
        # now print it out and add our comment
        print(u"San Diego Feels Like: {}{}F".format(current_temp, degree))
        if current_temp > 100:
            print("You Dead")
        elif 100 >= current_temp > 80:
            print("It's hot af")
        elif 80 >= current_temp > 70:
            print("It's just right")
        elif 70 >= current_temp > 50:
            print("It's kinda cool")
        else:
            print("It's cold af")
        # finally, wait 5 minutes (300 seconds) before updating again
        time.sleep(300)

if __name__ == "__main__":
    weather()

这篇关于对于循环功能,虽然真正难以理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆