【问题标题】:For loop in function, While true difficulty to understand函数中的for循环,虽然真正难懂
【发布时间】:2018-05-24 12:20:48
【问题描述】:

我将这个简单的Web Weather Scraping 脚本放在一起,以检查给定位置的温度。该代码运行良好,尽管它可能不是最好的或最干净的版本。还在学习。但它正在抓取:<span _ngcontent-c19="" class="wu-value wu-value-to">67</span> 来自 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 @ 987654328@等 但是,我很难理解如何告诉我的代码执行,或者在这种情况下print 您可以说这些不同的任务或方法?原谅我的英语。 While (True): 循环肯定有问题,但我无法理解它。感谢您的帮助,祝大家周日愉快。

#!/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()

【问题讨论】:

  • 我猜i 是一个字符串,但与另一个整数比较时应该是一个整数。

标签: python for-loop web-scraping beautifulsoup


【解决方案1】:

首先,即使您只对显示的值感兴趣,您也会收集整个&lt;span&gt; 标签,因此要获取温度值(并将其转换为实际整数),请执行以下操作:

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

其次,你的current_temp一旦获得就永远不会改变,你想要的是定期获取最新的温度值,然后根据它的值打印你想要的任何东西。比如:

# !/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()

【讨论】:

  • 哦,我现在明白了,这就解释了。如果没有 for 循环,这看起来会更好。感谢您的帮助和时间来解释这一点。我真的很感激。
  • 所以我也想知道这是否可以用于此目的? [这里] (stackoverflow.com/questions/47724574/…) 是我的比特币抓取,我可以使用相同的循环打印,例如:价格太高或价格太低,取决于价值如何变化?
  • @uzdisral - 我不明白为什么不这样做,前提是您从该比特币价值跟踪网站提取正确的字段。
  • 我也是这么想的。我一直在尝试,这里是代码pastebin.com/Mm6UenNW 第一个给我误报,第二个给我错误
  • @uzdisral - 现在这有点超出了这个范围的问题,并且确定;y 无法在 cmets 中解决。如果您有特定问题,请创建一个新问题。
猜你喜欢
  • 1970-01-01
  • 2014-06-12
  • 2011-12-03
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多