반응형
방금 파이썬을 배우기 시작했습니다. 코드를 올바르게 썼다고 확신합니다.
import urllib
import re
stockname = input('Enter the stock name : ')
url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol="+stockname+"&illiquid=0&smeFlag=0&itpFlag=0"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="lastPrice">'+stockname+'</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print (price)
하지만 아무리 노력해도이 오류가 계속 발생합니다.
Enter the stock name : tcs
Traceback (most recent call last):
File "C:\Users\.....\Desktop\stockq.py", line 8, in <module>
htmlfile = urllib.urlopen(url)
AttributeError: module 'urllib' has no attribute 'urlopen'
나는`urllib.request도 시도했다. 이 오류가 발생합니다.
Traceback (most recent call last):
File "C:\Users\HiMMi\Desktop\stockq.py", line 8, in <module>
htmlfile = urllib.request.urlopen(url)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
해결 방법
py2에서는 작동하지만 Python 3x에서는 작동하지 않습니다. Python 3 x의 경우 urlopen
이 urllib.request
에 있습니다.
import urllib.request
urllib.request.urlopen(...)
참조 페이지 https://stackoverflow.com/questions/39975367
반응형
'파이썬' 카테고리의 다른 글
파이썬 0에서 9 사이의 임의의 정수 생성 (0) | 2020.10.29 |
---|---|
파이썬 Python 3.6의 변수 주석은 무엇입니까? (0) | 2020.10.29 |
파이썬 Truthy와 Falsy는 무엇입니까? 참 및 거짓과 어떻게 다릅니 까? (0) | 2020.10.29 |
파이썬 Python-임시 파일에서 쓰기 및 읽기 (0) | 2020.10.28 |
파이썬 Eclipse에서 pep8.py를 통합하는 방법은 무엇입니까? (0) | 2020.10.28 |
댓글