Requests Headers

모바일 페이지로 요청하기


url = 'http://www.naver.com'
# pc
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
}

# mobile
headers = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)

## 응답 경로 확인
print(response.geturl()

Naver 뉴스 수집하기 안될 때


import requests
from bs4 import BeautifulSoup

url = 'https://n.news.naver.com/mnews/ranking/article/028/0002609157'
headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
response = requests.get(url,headers=headers)
print(response.status_code)

Daum 뉴스 AJAX 수집 안될 때


import requests
from bs4 import BeautifulSoup

url = 'https://comment.daum.net/apis/v1/ui/single/main/@20221006153907084?version=v3.17.2'
headers = {
   'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJncmFudF90eXBlIjoiYWxleF9jcmVkZW50aWFscyIsInNjb3BlIjpbXSwiZXhwIjoxNTY0Njc4NjQ1LCJhdXRob3JpdGllcyI6WyJST0xFX0NMSUVOVCJdLCJqdGkiOiJlZGUxNzM0MS1hNWNjLTRmYmQtODJkMy0zZTMwOGMwMGViZTEiLCJjbGllbnRfaWQiOiIyNkJYQXZLbnk1V0Y1WjA5bHI1azc3WTgifQ.Cxs2g1hUUAjyuSrUDAhaKGol8vvyW-_mwPtV0X0DvEU'
}
response = requests.get(url,headers=headers)
print(response.status_code)