쿼리스트링 생성
import request as rq
url = "https://sang-gamja.tistory.com/"
res = rq.get(url, params = { "key1" : "value1", "key2" : "value2"})
print(res.url)
json 모듈과 str 차이점
import json
dict1 = { 'key1' : 'value1', 'key2' : 'value2'}
print(json.dumps(dict1)) #{ "key1" : "value1" , "key2" : "value2" }
print(str(dict1)) #{ 'key1' : 'value1' , 'key2' : 'value2' }
헤더 설정하기
import requests as rq
url = "https://sang-gamja.tistory.com/"
res = rq.get(url, headers = {"User-Agent" : "Mozilla/5.0 {Macintosh; Intel Mac OS X 10_12_5) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"})
print(res.url)
requests 오류 처리 방법
import requests as rq
url = "https://sang-gamja.tistory.com"
try:
res = rq.get(url)
print(res.url)
except rq.exceptions.HTTPError:
print("HTTP 에러발생")