Commit 6527c883 authored by ^고맙습니다 _布德天下_ 감사합니다_^))//'s avatar ^고맙습니다 _布德天下_ 감사합니다_^))//
Browse files

표준 함수 urllib 실험 -- User-Agent 식별

parent 3c6d7972
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import time
from urllib.request import urlopen, Request
# 참고문헌: [0][1]
# [0] https://seunggabi.tistory.com/entry/Python-requests-UserAgent
# [1] https://stackoverflow.com/questions/24226781/changing-user-agent-in-python-3-for-urrlib-request-urlopen
FURL = """
https://httpbin.org/user-agent
"""
FURL = FURL.strip()
MH = """
Python 3 (Ubuntu 18.04)
"""
MH = MH.strip()
MH = {'User-Agent': MH}
def get_text(x, y=None):
try:
if y:
req = Request(x, headers=y)
else:
req = Request(x)
response = urlopen(req)
text = response.read().decode("utf-8", "replace")
except:
print(sys.exc_info()[0])
raise
return text
if __name__ == "__main__":
print(get_text(FURL))
time.sleep(3)
print(get_text(FURL, MH))
# 편집: VIM (Ubuntu 18.04)
# 마지막 갱신: 2021년 5월 5일
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment