답변:
urllib2
설명서에 명시된 바와 같이 :
이
urllib2
모듈은 Python 3에서 이름이urllib.request
이고 여러 모듈로 나누어 져 있습니다urllib.error
. 이2to3
도구는 소스를 Python 3으로 변환 할 때 가져 오기를 자동으로 조정합니다.
그래서 대신에
from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)
방금 편집하는 urllib.urlopen("http://www.google.com/")
대신 현재 편집중인 코드 샘플이 잘못되었습니다 urlopen("http://www.google.com/")
.
urllib.request
그렇지 않습니다 urllib2.request
. Python 2.x 의 urllib
및 urllib2
모듈은 Python urllib
3 의 모듈 로 결합되었습니다.
Python 2 (테스트 버전 2.7.3 및 2.6.8) 및 Python 3 (3.2.3 및 3.3.2+)에서 작동하는 스크립트의 경우 다음을 시도하십시오.
#! /usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
html = urlopen("http://www.google.com/")
print(html.read())
위의 3.3에서는 저에게 효과가 없었습니다. 대신 이것을보십시오 (YMMV 등)
import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))
Python 2와 Python 3의 패키지 내용을 보여주기 위해 일부 탭 완성.
파이썬 2에서 :
In [1]: import urllib
In [2]: urllib.
urllib.ContentTooShortError urllib.ftpwrapper urllib.socket urllib.test1
urllib.FancyURLopener urllib.getproxies urllib.splitattr urllib.thishost
urllib.MAXFTPCACHE urllib.getproxies_environment urllib.splithost urllib.time
urllib.URLopener urllib.i urllib.splitnport urllib.toBytes
urllib.addbase urllib.localhost urllib.splitpasswd urllib.unquote
urllib.addclosehook urllib.noheaders urllib.splitport urllib.unquote_plus
urllib.addinfo urllib.os urllib.splitquery urllib.unwrap
urllib.addinfourl urllib.pathname2url urllib.splittag urllib.url2pathname
urllib.always_safe urllib.proxy_bypass urllib.splittype urllib.urlcleanup
urllib.base64 urllib.proxy_bypass_environment urllib.splituser urllib.urlencode
urllib.basejoin urllib.quote urllib.splitvalue urllib.urlopen
urllib.c urllib.quote_plus urllib.ssl urllib.urlretrieve
urllib.ftpcache urllib.re urllib.string
urllib.ftperrors urllib.reporthook urllib.sys
파이썬 3에서 :
In [2]: import urllib.
urllib.error urllib.parse urllib.request urllib.response urllib.robotparser
In [2]: import urllib.error.
urllib.error.ContentTooShortError urllib.error.HTTPError urllib.error.URLError
In [2]: import urllib.parse.
urllib.parse.parse_qs urllib.parse.quote_plus urllib.parse.urldefrag urllib.parse.urlsplit
urllib.parse.parse_qsl urllib.parse.unquote urllib.parse.urlencode urllib.parse.urlunparse
urllib.parse.quote urllib.parse.unquote_plus urllib.parse.urljoin urllib.parse.urlunsplit
urllib.parse.quote_from_bytes urllib.parse.unquote_to_bytes urllib.parse.urlparse
In [2]: import urllib.request.
urllib.request.AbstractBasicAuthHandler urllib.request.HTTPSHandler
urllib.request.AbstractDigestAuthHandler urllib.request.OpenerDirector
urllib.request.BaseHandler urllib.request.ProxyBasicAuthHandler
urllib.request.CacheFTPHandler urllib.request.ProxyDigestAuthHandler
urllib.request.DataHandler urllib.request.ProxyHandler
urllib.request.FTPHandler urllib.request.Request
urllib.request.FancyURLopener urllib.request.URLopener
urllib.request.FileHandler urllib.request.UnknownHandler
urllib.request.HTTPBasicAuthHandler urllib.request.build_opener
urllib.request.HTTPCookieProcessor urllib.request.getproxies
urllib.request.HTTPDefaultErrorHandler urllib.request.install_opener
urllib.request.HTTPDigestAuthHandler urllib.request.pathname2url
urllib.request.HTTPErrorProcessor urllib.request.url2pathname
urllib.request.HTTPHandler urllib.request.urlcleanup
urllib.request.HTTPPasswordMgr urllib.request.urlopen
urllib.request.HTTPPasswordMgrWithDefaultRealm urllib.request.urlretrieve
urllib.request.HTTPRedirectHandler
In [2]: import urllib.response.
urllib.response.addbase urllib.response.addclosehook urllib.response.addinfo urllib.response.addinfourl
파이썬 3에서 텍스트 출력을 얻으려면 :
import io
import urllib.request
response = urllib.request.urlopen("http://google.com")
text = io.TextIOWrapper(response)
그것은 python3에서 나를 위해 일했습니다 :
import urllib.request
htmlfile = urllib.request.urlopen("http://google.com")
htmltext = htmlfile.read()
print(htmltext)
urllib.urlopen("http://www.google.com/")
대신 말하는 것입니다.urlopen("http://www.google.com/")