Đây là mã của tôi:
import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)
Có ai giúp đỡ không?
Đây là mã của tôi:
import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)
Có ai giúp đỡ không?
Câu trả lời:
Như đã nêu trong urllib2tài liệu :
Các
urllib2mô-đun đã được chia trên một số module trong Python 3 tênurllib.requestvàurllib.error. Công2to3cụ sẽ tự động điều chỉnh nhập khẩu khi chuyển đổi nguồn của bạn sang Python 3.
Vì vậy, thay vào đó bạn nên nói
from urllib.request import urlopen
html = urlopen("http://www.google.com/").read()
print(html)
Mẫu mã hiện tại, được chỉnh sửa của bạn là không chính xác vì bạn đang nói urllib.urlopen("http://www.google.com/")thay vì chỉ urlopen("http://www.google.com/").
urllib.requestvà không urllib2.request. Các mô-đun urllibvà urllib2từ Python 2.x đã được kết hợp thành urllibmô-đun trong Python 3.
                    Để biết tập lệnh hoạt động với Python 2 (phiên bản thử nghiệm 2.7.3 và 2.6.8) và Python 3 (3.2.3 và 3.3.2+), hãy thử:
#! /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())
Ở trên không làm việc cho tôi trong 3.3. Thay vào đó hãy thử điều này (YMMV, v.v.)
import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))
Một số tab hoàn thành để hiển thị nội dung của các gói trong Python 2 vs Python 3.
Trong Python 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  
Trong Python 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
Con trăn 3:
import urllib.request
wp = urllib.request.urlopen("http://google.com")
pw = wp.read()
print(pw)
Con trăn 2:
import urllib
import sys
wp = urllib.urlopen("http://google.com")
for line in wp:
    sys.stdout.write(line)
Trong khi tôi đã thử nghiệm cả hai Mã trong các phiên bản tương ứng.
Đơn giản nhất trong tất cả các giải pháp:
Trong Python 3.x:
import urllib.request
url = "https://api.github.com/users?since=100"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
data_content = response.read()
print(data_content)
Trong python 3, để có được đầu ra văn bản:
import io
import urllib.request
response = urllib.request.urlopen("http://google.com")
text = io.TextIOWrapper(response)
Điều đó làm việc cho tôi trong python3:
import urllib.request
htmlfile = urllib.request.urlopen("http://google.com")
htmltext = htmlfile.read()
print(htmltext)
urllib.urlopen("http://www.google.com/")thay vì chỉurlopen("http://www.google.com/")