この単純な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()
「i」は文字列ですが、別の整数と比較するときは整数である必要があります。 –