• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    해결됨

미세먼지부터 작동이 되지 않습니다.

21.07.01 20:54 작성 조회수 152

0

코드를 전부 입력하고 동작을 시켰는데 자꾸 

[오늘의 날씨]

Traceback (most recent call last):

  File "c:/Users/HP_20H2/Desktop/PythonWorkspace/webscraping/project.py", line 34, in <module>

    scrape_weather() #오늘의 날씨 정보 가져오기

  File "c:/Users/HP_20H2/Desktop/PythonWorkspace/webscraping/project.py", line 21, in scrape_weather

    pm10 = dust.find_all("dd")[0].get_text() # 미세먼지

AttributeError: 'NoneType' object has no attribute 'find_all'

PS C:\Users\HP_20H2\Desktop\PythonWorkspace>
로만 출력이 됩니다.

어느 부분에서 잘못 입력을 하였는지 알려주시면 감사하겠습니다.

import requests
from bs4 import BeautifulSoup

def scrape_weather():
    print("[오늘의 날씨]")
    url = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%EC%84%9C%EC%9A%B8+%EB%82%A0%EC%94%A8"
    res = requests.get(url)
    res.raise_for_status()
    soup = BeautifulSoup(res.text"lxml")
    # 맑음, 어제 기온과 같음
    cast = soup.find("p"attrs={"class":"cast_txt"}).get_text()
    # 현재 00도, (최저 / 최고)
    curr_temp = soup.find("p"attrs={"class":"info_temperature"}).get_text().replace("도씨"""# 현재온도
    min_temp = soup.find("span"attrs={"class":"min"}).get_text() # 최저 온도
    max_temp = soup.find("span"attrs={"class":"max"}).get_text() # 최고 온도
    # 오전 / 오후 강수 확률
    morning_rain_rate = soup.find("span"attrs={"class":"point_time morning"}).get_text().strip() # 오전 강수확률
    afternoon_rain_rate = soup.find("span"attrs={"class":"point_time afternoon"}).get_text().strip() # 오후 강수확률
    # 미세먼지 정보
    dust = soup.find("di"attrs={"class":"indicator"})
    pm10 = dust.find_all("dd")[0].get_text() # 미세먼지
    pm25 = dust.find_all("dd")[1].get_text() # 초미세먼지

    # 출력
    print(cast)
    print("현재 {} ( 최저 {} / 최고 {} )".format(curr_tempmin_tempmax_temp))
    print("오전 {} / 오후 {}".format(morning_rain_rateafternoon_rain_rate))
    print()
    print("미세먼지 {}".format(pm10))
    print("초미세먼지 {}".format(pm25))
    print()

if __name__ == "__main__":
    scrape_weather() #오늘의 날씨 정보 가져오기

답변 1

답변을 작성해보세요.

0

dl을 di라고 적었네요... 수정했습니다