Đã hỏi 9 năm, 8 tháng trước 9 years, 8 months ago
Đã xem 386k lần 386k times
Các hoạt động sau đây khi tôi dán nó lên trình duyệt:
//www.somesite.com/details.pl?urn=2344Nhưng khi tôi thử đọc URL với Python không có gì xảy ra:
link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfileTôi có cần phải mã hóa URL, hoặc có điều gì tôi không thấy không?
Martin Thoma
114K149 Huy hiệu vàng575 Huy hiệu bạc882 Huy hiệu Đồng149 gold badges575 silver badges882 bronze badges
hỏi ngày 28 tháng 2 năm 2013 lúc 14:55Feb 28, 2013 at 14:55
Helen Neelyhelen NeelyHelen Neely
4.5618 Huy hiệu vàng39 Huy hiệu bạc61 Huy hiệu Đồng8 gold badges39 silver badges61 bronze badges
Đối với người dùng link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 1, để tiết kiệm thời gian, sử dụng mã sau,
from urllib.request import urlopen link = "//docs.scipy.org/doc/numpy/user/basics.broadcasting.html" f = urlopen(link) myfile = f.read() print(myfile)Tôi biết có các chủ đề khác nhau cho lỗi: link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 2, nhưng nghĩ rằng điều này có thể tiết kiệm thời gian.
Asclepius
52.9K16 Huy hiệu vàng154 Huy hiệu bạc137 Huy hiệu đồng16 gold badges154 silver badges137 bronze badges
Đã trả lời ngày 25 tháng 8 năm 2017 lúc 17:38Aug 25, 2017 at 17:38
i.n.n.mi.n.n.mi.n.n.m
2.7886 Huy hiệu vàng25 Huy hiệu bạc 50 Huy hiệu Đồng6 gold badges25 silver badges50 bronze badges
2
Không có câu trả lời nào trong số này rất tốt cho Python 3 (được thử nghiệm trên phiên bản mới nhất tại thời điểm của bài đăng này).
Đây là cách bạn làm điều đó...
import urllib.request try: with urllib.request.urlopen('//www.python.org/') as f: print(f.read().decode('utf-8')) except urllib.error.URLError as e: print(e.reason)Trên đây là cho các nội dung trả về 'UTF-8'. Xóa .decode ('UTF-8') nếu bạn muốn Python "đoán mã hóa thích hợp".
Tài liệu: //docs.python.org/3/l Library/urllib.request.html#module-urllib.Request
Đã trả lời ngày 24 tháng 5 năm 2019 lúc 14:50May 24, 2019 at 14:50
FreddiefreddieFreddie
8001 Huy hiệu vàng10 Huy hiệu bạc22 Huy hiệu đồng1 gold badge10 silver badges22 bronze badges
1
Một giải pháp với các công việc với Python 2.x và Python 3.x sử dụng thư viện tương thích Python 2 và 3 link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 3:
from six.moves.urllib.request import urlopen link = "//www.somesite.com/details.pl?urn=2344" response = urlopen(link) content = response.read() print(content)Đã trả lời ngày 20 tháng 1 năm 2015 lúc 8:17Jan 20, 2015 at 8:17
Martin Thomamartin ThomaMartin Thoma
114K149 Huy hiệu vàng575 Huy hiệu bạc882 Huy hiệu Đồng149 gold badges575 silver badges882 bronze badges
hỏi ngày 28 tháng 2 năm 2013 lúc 14:55
from urllib.request import urlopen response = urlopen('//google.com/') html = response.read() print(html)Helen Neelyhelen NeelyMar 8, 2018 at 9:21
4.5618 Huy hiệu vàng39 Huy hiệu bạc61 Huy hiệu ĐồngAkash Kinwad
Đối với người dùng link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 1, để tiết kiệm thời gian, sử dụng mã sau,1 gold badge7 silver badges21 bronze badges
1
#!/usr/bin/python # -*- coding: utf-8 -*- # Works on python 3 and python 2. # when server knows where the request is coming from. import sys if sys.version_info[0] == 3: from urllib.request import urlopen else: from urllib import urlopen with urlopen('//www.facebook.com/') as \ url: data = url.read() print data # When the server does not know where the request is coming from. # Works on python 3. import urllib.request user_agent = \ 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7' url = '//www.facebook.com/' headers = {'User-Agent': user_agent} request = urllib.request.Request(url, None, headers) response = urllib.request.urlopen(request) data = response.read() print data
Tôi biết có các chủ đề khác nhau cho lỗi: link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 2, nhưng nghĩ rằng điều này có thể tiết kiệm thời gian.Aug 24, 2019 at 7:14
Asclepius
import urllib link = "//www.somesite.com/details.pl?urn=2344" f = urllib.urlopen(link) myfile = f.readline() print myfile52.9K16 Huy hiệu vàng154 Huy hiệu bạc137 Huy hiệu đồngFeb 28, 2013 at 14:58
Đã trả lời ngày 25 tháng 8 năm 2017 lúc 17:38ATOzTOA
2.7886 Huy hiệu vàng25 Huy hiệu bạc 50 Huy hiệu Đồng22 gold badges93 silver badges116 bronze badges
1
Không có câu trả lời nào trong số này rất tốt cho Python 3 (được thử nghiệm trên phiên bản mới nhất tại thời điểm của bài đăng này).
import urllib def read_text(): quotes = urllib.urlopen("//s3.amazonaws.com/udacity-hosted-downloads/ud036/movie_quotes.txt") contents_file = quotes.read() print contents_file read_text()Đây là cách bạn làm điều đó...Aug 22, 2017 at 11:00
# retrieving data from url # only for python 3 import urllib.request def main(): url = "//docs.python.org" # retrieving data from URL webUrl = urllib.request.urlopen(url) print("Result code: " + str(webUrl.getcode())) # print data from URL print("Returned data: -----------------") data = webUrl.read().decode("utf-8") print(data) if __name__ == "__main__": main()
Trên đây là cho các nội dung trả về 'UTF-8'. Xóa .decode ('UTF-8') nếu bạn muốn Python "đoán mã hóa thích hợp".Nov 27, 2019 at 7:37
link = '//www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 0
Tài liệu: //docs.python.org/3/l Library/urllib.request.html#module-urllib.Request
Đã trả lời ngày 24 tháng 5 năm 2019 lúc 14:502 gold badges21 silver badges38 bronze badges
FreddiefreddieMay 16, 2020 at 7:59
1