Mô-đun xác định các hàm và lớp xác định giao diện giống như tệp tối thiểu, bao gồm
>>> import urllib.request >>> req = urllib.request.Request[url='//localhost/cgi-bin/test.cgi', .. data=b'This data is passed to stdin of the CGI'] >>> with urllib.request.urlopen[req] as f: .. print[f.read[].decode['utf-8']] ... Got Data: "This data is passed to stdin of the CGI"76 và
>>> import urllib.request >>> req = urllib.request.Request[url='//localhost/cgi-bin/test.cgi', .. data=b'This data is passed to stdin of the CGI'] >>> with urllib.request.urlopen[req] as f: .. print[f.read[].decode['utf-8']] ... Got Data: "This data is passed to stdin of the CGI"77. Các chức năng được xác định bởi mô-đun này được sử dụng nội bộ bởi mô-đun. Đối tượng phản hồi điển hình là một thể hiện
Mô-đun này cung cấp các chức năng để chia nhỏ các URL thành các phần cấu thành của chúng, như được xác định bởi các RFC có liên quan
phân tích cú pháp
Giá trị trả về từ hàm là một đối tượng hoạt động giống như một bộ có 6 phần tử
from urlparse import urlparse parsed = urlparse['//netloc/path;parameters?query=argument#fragment'] print parsed
The parts of the URL available through the tuple interface are the scheme, network location, path, parameters, query, and fragment
$ python urlparse_urlparse.py ParseResult[scheme='http', netloc='netloc', path='/path', params='parameters', query='query=argument', fragment='fragment']
Mặc dù giá trị trả về hoạt động giống như một bộ, nhưng nó thực sự dựa trên một , một lớp con của bộ hỗ trợ truy cập các phần của URL thông qua các thuộc tính được đặt tên thay vì các chỉ mục. Điều đó đặc biệt hữu ích nếu, giống như tôi, bạn không thể nhớ thứ tự chỉ mục. Ngoài việc dễ sử dụng hơn cho lập trình viên, API thuộc tính còn cung cấp quyền truy cập vào một số giá trị không có sẵn trong API tuple
from urlparse import urlparse parsed = urlparse['//user:pass@NetLoc:80/path;parameters?query=argument#fragment'] print 'scheme :', parsed.scheme print 'netloc :', parsed.netloc print 'path :', parsed.path print 'params :', parsed.params print 'query :', parsed.query print 'fragment:', parsed.fragment print 'username:', parsed.username print 'password:', parsed.password print 'hostname:', parsed.hostname, '[netloc in lower case]' print 'port :', parsed.port
Tên người dùng và mật khẩu khả dụng khi có trong URL đầu vào và Không có khi không. Tên máy chủ có cùng giá trị với netloc, tất cả đều là chữ thường. Và cổng được chuyển đổi thành số nguyên khi có mặt và Không có khi không
$ python urlparse_urlparseattrs.py scheme : http netloc : user:pass@NetLoc:80 path : /path params : parameters query : query=argument fragment: fragment username: user password: pass hostname: netloc [netloc in lower case] port : 80
Hàm urlsplit[] là một thay thế cho. Nó hoạt động hơi khác một chút vì nó không tách các tham số khỏi URL. Điều này hữu ích cho các URL tuân theo RFC 2396, hỗ trợ các tham số cho từng phân đoạn của đường dẫn
from urlparse import urlsplit parsed = urlsplit['//user:pass@NetLoc:80/path;parameters/path2;parameters2?query=argument#fragment'] print parsed print 'scheme :', parsed.scheme print 'netloc :', parsed.netloc print 'path :', parsed.path print 'query :', parsed.query print 'fragment:', parsed.fragment print 'username:', parsed.username print 'password:', parsed.password print 'hostname:', parsed.hostname, '[netloc in lower case]' print 'port :', parsed.port
Vì các tham số không được tách ra, API tuple sẽ hiển thị 5 thành phần thay vì 6 và không có thuộc tính params
$ python urlparse_urlsplit.py SplitResult[scheme='http', netloc='user:pass@NetLoc:80', path='/path;parameters/path2;parameters2', query='query=argument', fragment='fragment'] scheme : http netloc : user:pass@NetLoc:80 path : /path;parameters/path2;parameters2 query : query=argument fragment: fragment username: user password: pass hostname: netloc [netloc in lower case] port : 80
Để chỉ cần tách mã định danh phân đoạn khỏi một URL, như bạn có thể cần làm để tìm tên trang cơ sở từ một URL, hãy sử dụng urldefrag[]