2017-12-11 1 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()) 

둘째, 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 is too high 또는 Price 값이 어떻게 변하는가에 따라 너무 낮습니다. – uzdisral

+0

@uzdisral - 왜 비트 코인 값 추적 사이트에서 적절한 필드를 추출해야하는지 알 수 없습니다. – zwer

관련 문제