2017-12-11 4 views
0

この単純な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を超える場合It is too hotなど しかし、実行するコードを教える方法を理解しているか、この場合は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() 
+1

「i」は文字列ですが、別の整数と比較するときは整数である必要があります。 –

答えて

1

まず第一に、あなたはその温度値を取得します(実際の整数に変換)するために、あなたが提示した値で唯一の興味さえカントー全体<span>タグを収集しています:

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

第2に、あなたの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() 
+0

オハイオ州、私は今それを説明しています。これはforループなしでさらに良く見えます。これを説明する助けと時間をありがとう。ほんとうにありがとう。 – uzdisral

+0

それで私がこれに使うことができるものがあるのだろうか? [Here](https://stackoverflow.com/questions/47724574/beautifulsoup-scraping-bitcoin-price-issue)は私のBitcoinスクレイピングです。たとえば、同じループを使用して印刷することはできますか?Price too too、またはPrice値がどのように変化するかによって、値が低すぎますか? – uzdisral

+0

@uzdisral - そのビットコイン値トラッキングサイトから適切なフィールドを抽出すると、私はなぜそうは見えないのですか? – zwer