반응형
requests 라이브러리를 이용해서 내 블로그에 대한 내용을 출력해보려고 하는 중 에러가 나왔다.
import requests
URL = 'http://orubt.tistory.com'
req = requests.get(URL)
html = req.text
status = req.status_code
print(status)
print()
print(html)
이 글을 보고 해결할 수 있었다.
https://stackoverflow.com/questions/38489386/python-requests-403-forbidden
페이지가 get 요청을 거절하기 때문에 user-agents를 바꿔준다.
User-Agents는 다음 URL에 접속해서 확인하면 된다.
https://developers.whatismybrowser.com/useragents/explore/
Windows 10에 크롬 브라우저를 사용하고 있기 때문에
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
를 사용했다.
import requests
URL = 'http://orubt.tistory.com'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
req = requests.get(URL, headers=headers)
html = req.text
status = req.status_code
print(status)
print()
print(html)
반응형
'에러 모음' 카테고리의 다른 글
AttributeError: module 'requests' has no attribute 'get' (1) | 2020.05.08 |
---|