Python 属性错误:模块';urllib';没有属性';urlopen';

Python 属性错误:模块';urllib';没有属性';urlopen';,python,web-scraping,Python,Web Scraping,我刚开始学习Python。我确信我写的代码是正确的 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=

我刚开始学习Python。我确信我写的代码是正确的

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)
导入urllib
进口稀土
stockname=input('输入股票名称:')
url=”https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=“+stockname+”&非流动性=0&SMEFAG=0&itpFlag=0”
htmlfile=urllib.urlopen(url)
htmltext=htmlfile.read()
正则表达式=“”+stockname+“”
pattern=re.compile(regex)
price=re.findall(模式,htmltext)
印刷品(价格)
但不管我怎么努力,我总是会犯这个错误

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'
输入股票名称:tcs
回溯(最近一次呼叫最后一次):
文件“C:\Users\…\Desktop\stockq.py”,第8行,在
htmlfile=urllib.urlopen(url)
AttributeError:模块“urllib”没有属性“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
回溯(最近一次呼叫最后一次):
文件“C:\Users\HiMMi\Desktop\stockq.py”,第8行,在
htmlfile=urllib.request.urlopen(url)
urlopen中的文件“C:\Users\HiMMi\AppData\Local\Programs\Python 36-32\lib\urllib\request.py”,第223行
返回opener.open(url、数据、超时)
文件“C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py”,第532行,打开
响应=方法(请求,响应)
http\U响应中的文件“C:\Users\HiMMi\AppData\Local\Programs\Python36-32\lib\urllib\request.py”,第642行
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py”第570行出错
返回自我。调用链(*args)
文件“C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py”,第504行,在调用链中
结果=func(*args)
文件“C:\Users\HiMMi\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py”,第650行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误403:禁止

这确实适用于py2,但不适用于Python 3x。对于Python 3x,
urlopen
出现在
urllib.request
中:

import urllib.request

urllib.request.urlopen(...)

它将在Python2的urllib2中…Python版本?@Abhishek我可以在Python2.7中
print(12)