DB에 데이터 저장하기
조회수 458회
def index(request):
req = requests.get('https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=%EC%84%B1%EB%82%A8%EC%8B%9C+%EC%88%98%EC%A0%95%EA%B5%AC+%EC%8B%A0%ED%9D%A5%EB%8F%99+%EB%82%A0%EC%94%A8&oquery=%EC%84%B1%EB%82%A8%EC%8B%9C+%EC%88%98%EC%A0%95%EA%B5%AC+%EB%82%A0%EC%94%A8&tqi=UC6hpdprvhGssb2o%2B3Vssssssrl-217403')
html = req.text
weather = BeautifulSoup(html, 'html.parser')
bs1 = weather.find('div' , class_ = 'main_info')
bs2 = bs1.find('div' , class_ = 'info_data')
bs3 = bs2.find('p' , class_ = 'info_temperature')
bs4 = bs3.find('span' , class_ = 'todaytemp')
temp = bs4.text
bs1 = weather.find('div' , class_ = 'info_list humidity _tabContent')
bs2 = bs1.find('ul' , class_ = 'list_area')
bs3 = bs2.find('li' , class_ = 'on now')
bs4 = bs3.find('dd' , class_ = 'weather_item _dotWrapper')
bs5 = bs4.find('span')
humi = bs5.text
a = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
Weather = {'Temperature':temp, 'Humidity':humi, 'time':a}
print("Temperature: %s°C & Humidity: %s%%" %(temp, humi))
#q = Weather(Temperature=temp, Humidity=humi, Check_Time=a)
#q.save()
# dic = {'Temperature' : temp, 'Humidity' : humi, 'Check_Time' : a}
# dic.save()
# Weather(Temperature=temp).save()
# Weather(Humidity=humi).save()
# Weather(Check_Time = a).save()
return render(request, 'Weather_data/index.html')
↑주석은 모두 다 실패한 방법입니다.
from django.db import models
# Create your models here.
class Weather(models.Model):
Temperature = models.CharField(max_length=20)
Humidity = models.CharField(max_length=20)
Check_Time = models.CharField(max_length=20)
여기서 크롤링한 온/습도 데이터를 어떻게 DB에 넣어야할지 잘 모르겠습니다.
오류에는 딕셔너리로 넣으라하니 딕셔너리로 하니 딕셔너리에는 save()란 기능이 없다는 오류가 다시 뜨네요.
원래는 웹에서 데이터를 전송받아 집어넣는건 해보았는데 views.py 안에서 데이터를 바로 넣는걸 모르겠습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력