코딩은 실력보다 시력이지

빅데이터교육과정/Python

DATA 긁어오기

Listeria 2021. 5. 25. 10:07

 

1. 만해 한용운 시인의 위키 내용 긁어오기

from bs4 import BeautifulSoup

import urllib.request as ur
import urllib.parse as up

url = "https://ko.wikisource.org/wiki/%EC%A0%80%EC%9E%90:%ED%95%9C%EC%9A%A9%EC%9A%B4"
data = ur.urlopen(url).read()
html = data.decode('utf-8')

soup = BeautifulSoup(html,'html.parser')

#data extract by using CSS query

data = soup.select('div.mw-parser-output > ul > li > a')

for dat in data :
    print(dat.string)

2. 네이버의 환율 정보 긁어오기

#CSS 선택자 사용하기

# BeautifulSoup.select_one(선택자) :CSS 선택자로 요소하나를 추출
# BeautifulSoup.select(선택자)

from bs4 import BeautifulSoup

import urllib.request as ur
import urllib.parse as up

url = "https://finance.naver.com/marketindex/"

data = ur.urlopen(url).read()
html = data.decode('euc-kr')

soup = BeautifulSoup(html,'html.parser')

#data extract by using CSS query


nation = soup.select('h3.h_lst > span.blind')
current = soup.select('span.value')
i=0
for nat in nation :
    print(nat.string)
    print(current[i].string)
    i+=1

3. 기상청 기상 정보 불러오기

import urllib.request as ur
import urllib.parse as up

rssUrl = "https://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp"

#stnId code
# 108 : 전국
# 109 : 서울/경기  105 : 강원도 131 : 충북
# 133 : 충남 146 : 전북   156:전남   143 : 경북  159:경남   184:제주도

values = {
    'stnId':'184'
}

param = up.urlencode(values)
url =rssUrl+"?"+param

data = ur.urlopen(url).read()
text = data.decode('utf-8')


print(text)

 

4. 특정 정보를 불러오고 저장하기

import urllib.request
url = "https://t1.daumcdn.net/daumtop_chanel/op/20200723055344399.png"
imgName = "D:/Programing/test2.png"

down_img = urllib.request.urlopen(url).read()

#파일 저장
# f = open("a.txt",w)
# f.write('내용')
# f.close()

# with open('a.txt','w') as f:
#      f.writ('내용')

with open(imgName,"wb") as f :
    f.write(down_img)

print("done")

'빅데이터교육과정 > Python' 카테고리의 다른 글

최종프로젝트 - AWS  (0) 2021.07.01
최종 프로젝트 - 이미지 학습(1)  (0) 2021.07.01
최종 프로젝트 - 자연어 처리  (0) 2021.07.01
파이썬 - 성적처리  (0) 2021.04.24