(파이썬) 프로그래밍 초보인데 갑갑한게 한두개가 아닙니다 ㅠㅠ

flyhs4444의 이미지

간단한 거 같은데 왜 안되는지ㅠ.ㅠ

제가 야후 웨더 api에서 날씨 정보를 받아와서 그 데이터를 쓰고 싶은데요. 어떤 프로그램을 사용자가 돌리면, 그 프로그램이 돌아가고 있는 현재 위치를 대략적으로라도 알아내서(경기? 서울?) 해당되는 날씨의 데이터를 받아서 따로 조건식을 만들어 가공하고 싶은데 어떻게 해야하는지 A부터 Z까지 모르겠네요 ㅠㅠ

또 하나는, datetime.datetime.now()를 이용해서 역시 현재 시간 데이터를 받아와서 쓰고싶어요......

도와주세요 능력자 분들 ㅠㅠ!

gumagula의 이미지

1. 날씨 정보를 어떤식으로 제공해 주는지는 확인을 안해봐 모르겠습니다만, xml형식이나 웹페이지 형식으로 제공된다면

BeautifulSoup등으로 가져오시면 됩니다. 관련 예제나 레퍼런스는 많이 있으니 한번 찾아보시구요,

2. 프로그램이 돌아가고 있는 현재 위치를 대략적으로 안다는 부분이 미묘한데요,

파이썬코드로 돌리시는 환경이 아마 PC일텐데, 활용 가능한 정보가 Ip를 통한 지역 유추 (?) 정도 밖에 없을 것 같습니다.

위젯이나 웹서핑 해보시면 ~~ ~~ 지역이라고 간략히 출력되는 방식이 Ip정보를 활용하여 지역을 알아내는 것인데,

정확도가 생각보단 잘나오긴 하지만, IP대역에 대한 지역정보를 가지고 있어야 활용이 가능 할 것입니다.

도움이 되셨으면 좋겠네요.

allinux의 이미지

rss 로 결과를 주네요

feedparser(https://code.google.com/p/feedparser/) 모듈을 설치하시고 아래와 같이 해보세요.

>>> import feedparser
>>> d = feedparser.parse("http://weather.yahooapis.com/forecastrss?w=2502265")
>>> d['feed']
{'image': {'height': 18,
'href': u'http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif',
'link': u'http://weather.yahoo.com',
'links': [{'href': u'http://weather.yahoo.com',
'rel': u'alternate',
'type': u'text/html'}],
'title': u'Yahoo! Weather',
'title_detail': {'base': u'http://weather.yahooapis.com/forecastrss?w=2502265',
'language': None,
'type': u'text/plain',
'value': u'Yahoo! Weather'},
'width': 142},
'language': u'en-us',
'link': u'http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html',
'links': [{'href': u'http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html',
'rel': u'alternate',
'type': u'text/html'}],
'subtitle': u'Yahoo! Weather for Sunnyvale, CA',
'subtitle_detail': {'base': u'http://weather.yahooapis.com/forecastrss?w=2502265',
'language': None,
'type': u'text/html',
'value': u'Yahoo! Weather for Sunnyvale, CA'},
'title': u'Yahoo! Weather - Sunnyvale, CA',
'title_detail': {'base': u'http://weather.yahooapis.com/forecastrss?w=2502265',
'language': None,
'type': u'text/plain',
'value': u'Yahoo! Weather - Sunnyvale, CA'},
'ttl': u'60',
'updated': u'Wed, 20 Nov 2013 11:56 pm PST',
'updated_parsed': time.struct_time(tm_year=2013, tm_mon=11, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=324, tm_isdst=0),
u'yweather_astronomy': {'sunrise': u'6:52 am', 'sunset': u'4:54 pm'},
u'yweather_atmosphere': {'humidity': u'93',
'pressure': u'29.92',
'rising': u'1',
'visibility': u'10'},
u'yweather_location': {'city': u'Sunnyvale',
'country': u'United States',
'region': u'CA'},
u'yweather_units': {'distance': u'mi',
'pressure': u'in',
'speed': u'mph',
'temperature': u'F'},
u'yweather_wind': {'chill': u'53', 'direction': u'0', 'speed': u'0'}}

chanik의 이미지

IP로부터 위치를 알아내는데는 MaxMind의 GeoIP를 쓸 수 있을 것입니다. API 안내가 http://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_APIs 에 있습니다. python 바인딩은 아래 페이지를 참고해 설치할 수 있습니다.

이 API들이 동작하려면 MaxMind에서 배포하는 DB가 필요합니다. 국가까지만 나오는 Country DB와 도시까지 나오는 City DB가 있습니다. 각각 유료버전도 있고 무료버전도 있습니다. 도시까지 나오는 무료버전의 이름은 GeoLite City 및 GeoLite2 City 입니다. 아래 페이지를 참고하시고요.

GeoLite City의 정확도 정보도 있는데 한국은 40km 이내에서 위치를 맞추는 확률이 59% 정도라고 합니다.

참고로 리눅스의 경우 배포판별로 GeoIP 관련 패키지가 몇 개씩 포함되어 있는데, 다들 구버전 API/DB인 것 같고 GeoIP2 기반의 패키지가 들어있는 경우는 못봤습니다 (CentOS 5.10, Ubuntu-12.04, Arch Linux 2013.10 모두 마찬가지). 구버전을 쓴다고 해서 위치정보의 질이 떨어지는지는 모르겠습니다.

검토해보시고 개발환경에 맞는 방향을 선택해서 쓰시면 되겠습니다.

TODO 1: 자신의 공인IP를 알아야 GeoIP 검색이 가능한데, 사설IP를 쓰면서 NAT 우산 아래 있는 PC의 경우엔 자신이 인터넷상에 어떤 공인IP로 노출되는지 모른다는 함정이 있습니다.

TODO 2: 우리나라만 대상으로 할 경우엔 GeoIP같은 글로벌DB보다는 국내의 통신사들이 제공하는 정확도 높은 DB가 있을 것 같은데요. 무료로 쓸 수 있는지 모르겠네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.