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
Python requests. 403 Forbidden
I needed to parse a site, but i got an error 403 Forbidden. Here is a code: url = 'http://worldagnetwork.com/' result = requests.get(url) print(result.content.decode()) Its output: ...
stackoverflow.com
페이지가 get 요청을 거절하기 때문에 user-agents를 바꿔준다.
User-Agents는 다음 URL에 접속해서 확인하면 된다.
https://developers.whatismybrowser.com/useragents/explore/
Browse our database of 28.9 million User Agents
There are millions of different types and formats of user agents, we've collected them and organised them by browser type, operating system, platform, software and hardware type. You can browse our huge collection here.
developers.whatismybrowser.com
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 |
---|