728x90
반응형

 

import sys
import io
import urllib.request as req
from urllib.parse import urlparse

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

url = "http://www.encar.com"

mem = req.urlopen(url)

# print(type(mem))
# python dict {}  , list [], tuple()  예습 필요


# print ("geturl", mem.geturl())
# print("status", mem.status)  # 응답값 200, 404, 403, 500 등 알아두어야 함.
# print("headers",mem.getheaders())

# print("info",mem.info())
# print("code", mem.getcode())
# print("read", mem.read(50).decode("utf-8"))  #옛날 사이트로 되있다면 euc-kr 등으로 디코딩함.

print(urlparse(url))

 

import sys
import io
import urllib.request as req
from urllib.parse import urlencode

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

#구글 검색 my ip api
# https://www.ipify.org/ -- 출처


API = "https://api.ipify.org"

values = {
'format' : 'json'
}

print('before',values)
params = urlencode(values)
print('after',params)

url = API + "?" + params
print("요청 url", url)

reqData = req.urlopen(url).read().decode('utf-8')
print("출력",reqData)

 

 

숙제 

import sys
import io
import urllib.request as req
from urllib.parse import urlencode

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

#구글 검색 my ip api
# https://www.ipify.org/ -- 출처

imgUrl1 ="https://ssl.pstatic.net/tveta/libs/1242/1242071/7263bcd2e96061265636_20190705115802112.jpg"
adUrl2 = "https://ssl.pstatic.net/tveta/libs/1239/1239407/92a189eb83ec9dcf5684_20190503133602254.jpg"

savePath1 ="homework.jpg"
savePath2 ="homework2.jpg"

f1 = req.urlopen(imgUrl1).read()  #이미지를 읽어와 f 에 저장
f2 = req.urlopen(adUrl2).read()

# saveFile1 = open(savePath1, 'wb')  # w: write, r: read, a: add  b:binary
# saveFile2 = open(savePath2, 'wb')
# saveFile1.write(f1)  #saveFile에 f의 데이터를 씀
# saveFile1.close()  #항상 리소스를 반납해줘야 하는 것 잊지 말것

# 이 아래 with 문을 자주 쓰는게 더 좋다고 함.
# 파이썬 2.7버전 이후로만 지원되며 with 문을 벗어나면 자동으로 자원 반환함

with open(savePath1, 'wb') as saveFile1:
    saveFile1.write(f1)

with open(savePath2, 'wb') as saveFile2:
    saveFile2.write(f2)


print("다운로드 완료!")

 

 

 

 

반응형

+ Recent posts