728x90
01. requests로 웹페이지 접속
import requests as rq
url = "https://chuuvelop.tistory.com/"
rq.get(url)
<Response [200]>
rq.post(url)
<Response [405]>
url = "https://chuuvelop.tistory.com/a"
res = rq.get(url) # 없는 주소로 요청
# 응답코드 가져오기
print(res.status_code)
404
url = "https://chuuvelop.tistory.com/"
res = rq.get(url)
print(res.status_code)
200
# 헤더 가져오기
print(res)
print(res.headers)
<Response [200]>
{'date': 'Wed, 15 Jan 2025 11:14:08 GMT', 'content-type': 'text/html;charset=UTF-8', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 't_userid': '6bb5f2b94c8cf8c9fdd93d7997ef3acd318909b7', 'set-cookie': 'REACTION_GUEST=d0c2c57a0a794a98fc8484569edc2c38c30ee7ff', 'x-content-type-options': 'nosniff', 'x-xss-protection': '0', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', 'pragma': 'no-cache', 'expires': '0', 'strict-transport-security': 'max-age=31536000 ; includeSubDomains', 'content-encoding': 'gzip'}
#html 코드 보기
print(res.text)
html코드가 출력됨
# 바이너리 형태로 값을 가져오기 때문에 res.text(상기결과) 에서 한글이 깨지는 현상을 방지할 수 있음
print(res.content)
바이너리 형태로 가져온 값이 출력됨
# 인코딩 확인
res.encoding
'UTF-8'
#rq.get("https://chuuvelop.tistory.com/?page=1") ##쿼리 뒤에 &로 이어져있으면 수정이 어려움
# 쿼리스트링 데이터 생성
res = rq.get(url, params = {"key1" : "value1", "key2" : "value2"})
print(res.url) #요청 보내는 URL출력
https://chuuvelop.tistory.com/?key1=value1&key2=value2
02. urllib으로 웹페이지 접속
from urllib.request import urlopen
url = "https://chuuvelop.tistory.com/"
page = urlopen(url)
print(page)
<http.client.HTTPResponse object at 0x000002D190366F80>
print(page.code)
print(page.headers)
print(page.url)
print(page.info().get_content_charset())
200
date: Wed, 15 Jan 2025 11:14:54 GMT
content-type: text/html;charset=UTF-8
content-length: 69626
vary: Accept-Encoding
t_userid: 0b8d28b34ccb54e42ffda9125c3508888cf27392
set-cookie: REACTION_GUEST=d9f675e2c8220ef01314f29e48b6133d864d797a
x-content-type-options: nosniff
x-xss-protection: 0
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
strict-transport-security: max-age=31536000 ; includeSubDomains
connection: close
https://chuuvelop.tistory.com/
utf-8
# requests의 content() 함수와 동일한 기능
print(page.read())
바이너리 형태로 가져온 값이 출력됨
728x90