Hướng dẫn how to split url in python - cách tách url trong python

Tôi có một danh sách lớn các URL như thế này:

http://www.example.com/site/section1/VAR1/VAR2

Trong đó VAR1 và VAR2 là các phần tử động của URL. Những gì tôi muốn làm là trích xuất từ ​​chuỗi URL này chỉ var1. Tôi đã cố gắng sử dụng urlparse nhưng đầu ra trông như thế này:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')

Đã hỏi ngày 1 tháng 7 năm 2015 lúc 19:39Jul 1, 2015 at 19:39

Hướng dẫn how to split url in python - cách tách url trong python

Ngoài ra, bạn có thể áp dụng phương pháp

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
3:

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']

Đã trả lời ngày 1 tháng 7 năm 2015 lúc 19:40Jul 1, 2015 at 19:40

Hướng dẫn how to split url in python - cách tách url trong python

Alecxealecxealecxe

449K114 Huy hiệu vàng1043 Huy hiệu bạc1167 Huy hiệu đồng114 gold badges1043 silver badges1167 bronze badges

1

Bạn có thể nhớ điều này nói chung. Các phần khác nhau của URL có thể thu được bằng cách sử dụng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
4. Tại đây bạn có thể lấy
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
5 bằng
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
6 và sau đó lấy biến mong muốn bằng hàm
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
3

>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'

Đã trả lời ngày 1 tháng 7 năm 2015 lúc 19:41Jul 1, 2015 at 19:41

Hướng dẫn how to split url in python - cách tách url trong python

Naman Soganinaman SoganiNaman Sogani

9431 Huy hiệu vàng7 Huy hiệu bạc28 Huy hiệu đồng1 gold badge7 silver badges28 bronze badges

1

Kiểm tra cái này, nó khá hiệu quả vì nó bắt đầu từ cuối chuỗi với tùy chọn MAXSplit, chúng ta có thể dừng số lần chia tách.

Cuối cùng, bạn có thể sử dụng lập chỉ mục để có được hai phần cuối của URL

>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']

Đã trả lời ngày 1 tháng 7 năm 2015 lúc 20:01Jul 1, 2015 at 20:01

Tôi chỉ đơn giản là thử

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]

Đã trả lời ngày 1 tháng 7 năm 2015 lúc 19:42Jul 1, 2015 at 19:42

QMorganqmorganqmorgan

4.5702 Huy hiệu vàng18 Huy hiệu bạc14 Huy hiệu đồng2 gold badges18 silver badges14 bronze badges

Mã nguồn: lib/urllib/parse.py Lib/urllib/parse.py


Mô -đun này xác định một giao diện tiêu chuẩn để phá vỡ các chuỗi định vị tài nguyên đồng đều (URL) trong các thành phần (sơ đồ giải quyết, vị trí mạng, đường dẫn, v.v.), để kết hợp các thành phần trở lại vào chuỗi URL và để chuyển đổi một URL tương đối thành URL tuyệt đối đã cho một url cơ sở của người Viking.

Mô -đun đã được thiết kế để phù hợp với RFC Internet trên các bộ định vị tài nguyên đồng nhất tương đối. Nó hỗ trợ các sơ đồ url sau:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
8,
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
9,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
0,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
1,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
2,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
3,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
4,
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
5 ,
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
0,
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
1,
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
2,
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
3.

Mô -đun

>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
4 xác định các chức năng rơi vào hai loại rộng: trích dẫn phân tích URL và URL. Chúng được đề cập chi tiết trong các phần sau.

Phân tích cú pháp url

Các chức năng phân tích URL tập trung vào việc chia chuỗi URL vào các thành phần của nó hoặc kết hợp các thành phần URL thành chuỗi URL.

________ 45 ________ 14 (urlString, speeme = '', allow_fragments = true) ¶(urlstring, scheme='', allow_fragments=True)

Phân tích một URL thành sáu thành phần, trả lại một mục gồm 6 mục tên là Tuple. Điều này tương ứng với cấu trúc chung của URL:

>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
7. Mỗi mục tuple là một chuỗi, có thể trống. Các thành phần không được chia thành các phần nhỏ hơn (ví dụ: vị trí mạng là một chuỗi đơn) và % thoát không được mở rộng. Các dấu phân cách như được hiển thị ở trên không phải là một phần của kết quả, ngoại trừ một dấu gạch chéo hàng đầu trong thành phần đường dẫn, được giữ lại nếu có. Ví dụ:named tuple. This corresponds to the general structure of a URL:
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
7. Each tuple item is a string, possibly empty. The components are not broken up into smaller parts (for example, the network location is a single string), and % escapes are not expanded. The delimiters as shown above are not part of the result, except for a leading slash in the path component, which is retained if present. For example:

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'

Theo thông số kỹ thuật của cú pháp trong RFC 1808, UrlParse chỉ nhận ra NETLOC nếu nó được giới thiệu đúng bởi ‘//. Nếu không, đầu vào được coi là một URL tương đối và do đó bắt đầu với một thành phần đường dẫn.RFC 1808, urlparse recognizes a netloc only if it is properly introduced by ‘//’. Otherwise the input is presumed to be a relative URL and thus to start with a path component.

>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')

Đối số sơ đồ cung cấp sơ đồ địa chỉ mặc định, chỉ được sử dụng nếu URL không chỉ định một. Nó phải là cùng loại (văn bản hoặc byte) như URLString, ngoại trừ giá trị mặc định

>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
8 luôn được phép và được tự động chuyển đổi thành
>>> url.rsplit('/',2)[1:]
['VAR1', 'VAR2']
9 nếu thích hợp.

Nếu đối số allow_fragments là sai, các định danh phân đoạn không được công nhận. Thay vào đó, chúng được phân tích cú pháp như một phần của đường dẫn, tham số hoặc thành phần truy vấn và

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
0 được đặt thành chuỗi trống trong giá trị trả về.

Giá trị trả về là một tuple được đặt tên, có nghĩa là các mục của nó có thể được truy cập bằng chỉ mục hoặc như các thuộc tính được đặt tên, đó là:named tuple, which means that its items can be accessed by index or as named attributes, which are:

Thuộc tính

Mục lục

Giá trị

Giá trị nếu không có mặt

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
1

0

Đặc biệt sơ đồ URL

tham số sơ đồ

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
2

1

Phần vị trí mạng

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
3

2

Con đường phân cấp

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
4

3

Con đường phân cấp

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
5

4

Con đường phân cấp

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
0

5

Con đường phân cấp

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
7

Con đường phân cấp

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
9

Tham số cho phần tử đường dẫn cuối cùng

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
1

Thành phần truy vấn

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
3

Định danh mảnh

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

Tên tài khoảnStructured Parse Results for more information on the result object.

Mật khẩu

Tên máy chủ (chữ thường)

Số cổng dưới dạng số nguyên, nếu có

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')

Đọc thuộc tính

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
3 sẽ tăng
>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6 nếu một cổng không hợp lệ được chỉ định trong URL. Xem phần Kết quả phân tích phân tích để biết thêm thông tin về đối tượng kết quả.Added IPv6 URL parsing capabilities.

Các dấu ngoặc vuông chưa từng có trong thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
2 sẽ tăng
>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.The fragment is now parsed for all URL schemes (unless allow_fragment is false), in accordance with RFC 3986. Previously, an allowlist of schemes that support fragments existed.

Các ký tự trong thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
2 phân tách theo chuẩn hóa NFKC (được sử dụng bởi mã hóa IDNA) thành bất kỳ
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
0,
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
1,
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
2,
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
3 hoặc
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
4 sẽ tăng
>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6. Nếu URL bị phân tách trước khi phân tích cú pháp, sẽ không có lỗi nào.Out-of-range port numbers now raise
>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6, instead of returning
url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8.

Như trường hợp với tất cả các bộ dữ liệu có tên, lớp con có một vài phương thức và thuộc tính bổ sung đặc biệt hữu ích. Một phương pháp như vậy là

>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
6. Phương thức
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
6 sẽ trả về một đối tượng phân tích cú pháp mới thay thế các trường được chỉ định bằng các giá trị mới.Characters that affect netloc parsing under NFKC normalization will now raise
>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.

Đã thay đổi trong phiên bản 3.2: Đã thêm khả năng phân tích cú pháp URL IPv6.(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Đã thay đổi trong phiên bản 3.3: Đoạn hiện được phân tích cú pháp cho tất cả các sơ đồ URL (trừ khi allow_fragment là sai), theo RFC 3986. Trước đây, một danh sách cho phép các sơ đồ hỗ trợ các mảnh tồn tại.

Đã thay đổi trong phiên bản 3.6: Số cổng ngoài phạm vi hiện đang tăng

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6, thay vì trả về
url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8.

Đã thay đổi trong phiên bản 3.8: Các ký tự ảnh hưởng đến phân tích cú pháp NetLoc theo chuẩn hóa NFKC giờ đây sẽ tăng

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.

________ 45 ________ 82 (

Phân tích một chuỗi truy vấn được đưa ra dưới dạng đối số chuỗi (dữ liệu của loại ứng dụng/x-www-form-urlencoded). Dữ liệu được trả về dưới dạng từ điển. Các khóa từ điển là tên biến truy vấn duy nhất và các giá trị là danh sách các giá trị cho mỗi tên.

Bộ phân cách đối số tùy chọn là biểu tượng để sử dụng để phân tách các đối số truy vấn. Nó mặc định là

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6.

Sử dụng hàm

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
7 (với tham số
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
8 được đặt thành
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
9) để chuyển đổi từ điển đó thành các chuỗi truy vấn.

Đã thay đổi trong phiên bản 3.2: Thêm tham số mã hóa và lỗi.Add encoding and errors parameters.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số MAX_NUM_FIELDS.Added max_num_fields parameter.

Đã thay đổi trong phiên bản 3.10: Đã thêm tham số phân tách với giá trị mặc định là

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6. Các phiên bản Python sớm hơn Python 3.10 cho phép sử dụng cả
(addressing scheme, network location, path, query, fragment identifier).
1 và
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 làm dấu phân cách tham số truy vấn. Điều này đã được thay đổi để chỉ cho phép một phím phân tách duy nhất, với
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 làm dấu phân cách mặc định.Added separator parameter with the default value of
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6. Python versions earlier than Python 3.10 allowed using both
(addressing scheme, network location, path, query, fragment identifier).
1 and
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 as query parameter separator. This has been changed to allow only a single separator key, with
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 as the default separator.

________ 45 ________ 95 ((qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Phân tích một chuỗi truy vấn được đưa ra dưới dạng đối số chuỗi (dữ liệu của loại ứng dụng/x-www-form-urlencoded). Dữ liệu được trả về dưới dạng danh sách tên, cặp giá trị.

Đối số tùy chọn giữ_blank_values ​​là một lá cờ cho biết liệu các giá trị trống trong các truy vấn được mã hóa phần trăm có nên được coi là các chuỗi trống hay không. Một giá trị thực sự chỉ ra rằng khoảng trống nên được giữ lại dưới dạng các chuỗi trống. Giá trị sai mặc định chỉ ra rằng các giá trị trống sẽ bị bỏ qua và xử lý như thể chúng không được bao gồm.

Đối số tùy chọn strict_parsing là một cờ chỉ ra những gì phải làm với các lỗi phân tích cú pháp. Nếu sai (mặc định), các lỗi bị bỏ qua âm thầm. Nếu đúng, lỗi sẽ tăng ngoại lệ

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.

Các tham số mã hóa và lỗi tùy chọn chỉ định cách giải mã các chuỗi được mã hóa phần trăm thành các ký tự unicode, được chấp nhận bởi phương thức

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
4.

Đối số tùy chọn max_num_fields là số trường tối đa để đọc. Nếu được đặt, thì hãy ném

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6 nếu có nhiều hơn các trường MAX_NUM_FIELDS được đọc.

Bộ phân cách đối số tùy chọn là biểu tượng để sử dụng để phân tách các đối số truy vấn. Nó mặc định là

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6.

Sử dụng chức năng

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
7 để chuyển đổi danh sách các cặp như vậy thành các chuỗi truy vấn.

Đã thay đổi trong phiên bản 3.2: Thêm tham số mã hóa và lỗi.Add encoding and errors parameters.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số MAX_NUM_FIELDS.Added max_num_fields parameter.

Đã thay đổi trong phiên bản 3.10: Đã thêm tham số phân tách với giá trị mặc định là

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6. Các phiên bản Python sớm hơn Python 3.10 cho phép sử dụng cả
(addressing scheme, network location, path, query, fragment identifier).
1 và
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 làm dấu phân cách tham số truy vấn. Điều này đã được thay đổi để chỉ cho phép một phím phân tách duy nhất, với
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 làm dấu phân cách mặc định.Added separator parameter with the default value of
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6. Python versions earlier than Python 3.10 allowed using both
(addressing scheme, network location, path, query, fragment identifier).
1 and
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 as query parameter separator. This has been changed to allow only a single separator key, with
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
6 as the default separator.

________ 45 ________ 95 ((parts)

Phân tích một chuỗi truy vấn được đưa ra dưới dạng đối số chuỗi (dữ liệu của loại ứng dụng/x-www-form-urlencoded). Dữ liệu được trả về dưới dạng danh sách tên, cặp giá trị.

Đối số tùy chọn giữ_blank_values ​​là một lá cờ cho biết liệu các giá trị trống trong các truy vấn được mã hóa phần trăm có nên được coi là các chuỗi trống hay không. Một giá trị thực sự chỉ ra rằng khoảng trống nên được giữ lại dưới dạng các chuỗi trống. Giá trị sai mặc định chỉ ra rằng các giá trị trống sẽ bị bỏ qua và xử lý như thể chúng không được bao gồm.(urlstring, scheme='', allow_fragments=True)

Đối số tùy chọn strict_parsing là một cờ chỉ ra những gì phải làm với các lỗi phân tích cú pháp. Nếu sai (mặc định), các lỗi bị bỏ qua âm thầm. Nếu đúng, lỗi sẽ tăng ngoại lệ

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.RFC 2396) is wanted. A separate function is needed to separate the path segments and parameters. This function returns a 5-item named tuple:

(addressing scheme, network location, path, query, fragment identifier).

Các tham số mã hóa và lỗi tùy chọn chỉ định cách giải mã các chuỗi được mã hóa phần trăm thành các ký tự unicode, được chấp nhận bởi phương thức

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
4.named tuple, its items can be accessed by index or as named attributes:

Đối số tùy chọn max_num_fields là số trường tối đa để đọc. Nếu được đặt, thì hãy ném

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6 nếu có nhiều hơn các trường MAX_NUM_FIELDS được đọc.

Sử dụng chức năng

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
7 để chuyển đổi danh sách các cặp như vậy thành các chuỗi truy vấn.

________ 45 ________ 106 (Bộ phận) ¶

Xây dựng một URL từ một tuple như được trả về bởi

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07. Các đối số phần có thể là bất kỳ điều gì có thể điều chỉnh được sáu mục. Điều này có thể dẫn đến một URL hơi khác nhau, nhưng tương đương, nếu URL được phân tích cú pháp ban đầu có các phân định không cần thiết (ví dụ:
>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')
1 với một truy vấn trống; RFC nói rằng đây là tương đương).

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
1

0

________ 45 ________ 110 (urlString, speeme = '', allow_fragments = true) ¶

Điều này tương tự như

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07, nhưng không phân chia các thông số từ URL. Điều này thường nên được sử dụng thay vì
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 nếu cú ​​pháp URL gần đây hơn cho phép các tham số được áp dụng cho từng phân đoạn của phần đường dẫn của URL (xem RFC 2396) là muốn. Một hàm riêng là cần thiết để tách các phân đoạn và tham số đường dẫn. Chức năng này trả về một Tuple có tên 5 mục:

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
2

1

Giá trị trả về là một tuple được đặt tên, các mục của nó có thể được truy cập bằng chỉ mục hoặc như các thuộc tính được đặt tên:

Thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
3

2

Mục lục

Thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
5

3

Mục lục

Thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
0

4

Mục lục

Thuộc tính

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
7

Mục lục

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
9

Giá trị

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
1

Giá trị nếu không có mặt

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
3

Đặc biệt sơ đồ URL

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8

tham số sơ đồStructured Parse Results for more information on the result object.

Phần vị trí mạng

Chuỗi trống

Con đường phân cấp

Thành phần truy vấnOut-of-range port numbers now raise

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6, instead of returning
url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
8.

Định danh mảnhCharacters that affect netloc parsing under NFKC normalization will now raise

>>> from urllib.parse import urlparse
>>> urlparse("scheme://netloc/path;parameters?query#fragment")
ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='',
            query='query', fragment='fragment')
>>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?"
...              "highlight=params#url-parsing")
>>> o
ParseResult(scheme='http', netloc='docs.python.org:80',
            path='/3/library/urllib.parse.html', params='',
            query='highlight=params', fragment='url-parsing')
>>> o.scheme
'http'
>>> o.netloc
'docs.python.org:80'
>>> o.hostname
'docs.python.org'
>>> o.port
80
>>> o._replace(fragment="").geturl()
'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
6.

Đã thay đổi trong phiên bản 3.10: Các ký tự Tab Newline và Tab ASCII bị tước khỏi URL.ASCII newline and tab characters are stripped from the URL.

________ 45 ________ 144 (Bộ phận) ¶(parts)

Kết hợp các phần tử của một tuple như được trả về bởi

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 thành một url hoàn chỉnh dưới dạng chuỗi. Các đối số phần có thể là bất kỳ mục nào có thể lặp lại. Điều này có thể dẫn đến một URL hơi khác nhau, nhưng tương đương, nếu URL được phân tích cú pháp ban đầu có các phân định không cần thiết (ví dụ: a? Với một truy vấn trống; RFC nói rằng đây là tương đương).

________ 45 ________ 147 (cơ sở, url, allow_fragments = true) ¶(base, url, allow_fragments=True)

Xây dựng một URL đầy đủ (tuyệt đối tuyệt đối) bằng cách kết hợp một URL cơ sở (cơ sở) với một URL khác (URL). Về mặt không chính thức, điều này sử dụng các thành phần của URL cơ sở, đặc biệt là sơ đồ địa chỉ, vị trí mạng và (một phần) đường dẫn, để cung cấp các thành phần bị thiếu trong URL tương đối. Ví dụ:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
0

Đối số allow_fragments có cùng ý nghĩa và mặc định như đối với

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07.

Ghi chú

Nếu URL là một URL tuyệt đối (nghĩa là, nó bắt đầu bằng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
49 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
50), tên máy chủ và/hoặc sơ đồ URL URL sẽ có mặt trong kết quả. Ví dụ:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
1

Nếu bạn không muốn hành vi đó, hãy xử lý trước URL với

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
52, loại bỏ sơ đồ có thể và các bộ phận Netloc.

Đã thay đổi trong phiên bản 3.5: Hành vi được cập nhật để phù hợp với ngữ nghĩa được xác định trong RFC 3986.Behavior updated to match the semantics defined in RFC 3986.

________ 45 ________ 154 (URL) ¶(url)

Nếu URL chứa một định danh đoạn, hãy trả về phiên bản URL được sửa đổi không có định danh phân đoạn và định danh đoạn là một chuỗi riêng biệt. Nếu không có định danh phân đoạn trong URL, hãy trả về URL không được sửa đổi và một chuỗi trống.

Giá trị trả về là một tuple được đặt tên, các mục của nó có thể được truy cập bằng chỉ mục hoặc như các thuộc tính được đặt tên:named tuple, its items can be accessed by index or as named attributes:

Thuộc tính

Mục lục

Giá trị

Giá trị nếu không có mặt

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
55

0

URL không có mảnh vỡ

Chuỗi trống

url = 'http://www.example.com/site/section1/VAR1/VAR2'
var1 = url.split('/')[-2]
0

1

Định danh mảnh

Chuỗi trống

Định danh mảnhStructured Parse Results for more information on the result object.

Xem phần Kết quả phân tích phân tích để biết thêm thông tin về đối tượng kết quả.Result is a structured object rather than a simple 2-tuple.

Thay đổi trong phiên bản 3.2: Kết quả là một đối tượng có cấu trúc chứ không phải là 2-tuple đơn giản.(url)

________ 45 ________ 158 (URL) ¶

Trích xuất URL từ URL được bọc (nghĩa là một chuỗi được định dạng là ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='') 59, ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='') 60, ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='') 61 hoặc ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='') 62). Nếu URL không phải là URL được bọc, nó sẽ được trả về mà không thay đổi.

Phân tích cú pháp ascii được mã hóa byte¶

Các chức năng phân tích URL ban đầu được thiết kế để chỉ hoạt động trên các chuỗi ký tự. Trong thực tế, rất hữu ích để có thể thao tác các URL được trích dẫn và mã hóa đúng cách như các chuỗi của các byte ASCII. Theo đó, các chức năng phân tích cú pháp URL trong mô -đun này đều hoạt động trên các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64 ngoài các đối tượng
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65.

Nếu dữ liệu

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 được truyền vào, kết quả cũng sẽ chỉ chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Nếu dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64 được truyền vào, kết quả sẽ chỉ chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63.

Cố gắng trộn dữ liệu

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 với
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64 trong một cuộc gọi chức năng sẽ dẫn đến một
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
74 được nâng lên, trong khi cố gắng vượt qua các giá trị byte không ASCII sẽ kích hoạt
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
75.

Để hỗ trợ chuyển đổi dễ dàng hơn các đối tượng kết quả giữa

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63, tất cả các giá trị trả về từ các hàm phân tích cú pháp URL cung cấp phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 (khi kết quả chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65) hoặc phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 (khi kết quả chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63). Chữ ký của các phương pháp này khớp với các phương thức tương ứng
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 (ngoại trừ mã hóa mặc định là
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
84 thay vì
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
85). Mỗi sản xuất một giá trị của một loại tương ứng chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 (đối với các phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78) hoặc dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 (đối với các phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80).

Các ứng dụng cần hoạt động trên các URL được trích dẫn không đúng cách có thể chứa dữ liệu không phải ASCII sẽ cần phải tự giải mã từ byte đến ký tự trước khi gọi các phương thức phân tích cú pháp URL.

Hành vi được mô tả trong phần này chỉ áp dụng cho các chức năng phân tích URL. Các chức năng trích dẫn URL sử dụng các quy tắc của riêng họ khi tạo hoặc tiêu thụ các chuỗi byte như được nêu chi tiết trong tài liệu của các chức năng trích dẫn URL riêng lẻ.URL parsing functions now accept ASCII encoded byte sequences

Đã thay đổi trong phiên bản 3.2: Các hàm phân tích cú pháp URL hiện đang chấp nhận ASCII được mã hóa chuỗi byte

Kết quả phân tích có cấu trúc

Các đối tượng kết quả từ các hàm
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07,
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92 là các lớp con của loại
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
93. Các lớp con này thêm các thuộc tính được liệt kê trong tài liệu cho các chức năng đó, hỗ trợ mã hóa và giải mã được mô tả trong phần trước, cũng như một phương thức bổ sung:()

________ 194 ________ 195 ()

Đối với kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92, chỉ có số nhận dạng đoạn trống sẽ bị xóa. Đối với kết quả
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 và
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07, tất cả các thay đổi được ghi nhận sẽ được thực hiện cho URL được trả về bằng phương pháp này.

Kết quả của phương pháp này vẫn không thay đổi nếu được truyền lại thông qua chức năng phân tích cú pháp gốc:

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
2

Các lớp sau đây cung cấp việc triển khai kết quả phân tích phân tích có cấu trúc khi hoạt động trên các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65:

Lớp ________ 45 ________ 201 (URL, Fragment) ¶(url, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một ví dụ
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
05.

Mới trong phiên bản 3.2.

Lớp ________ 45 ________ 207 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶(scheme, netloc, path, params, query, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
11.

Lớp ________ 45 ________ 213 (Sơ đồ, Netloc, Path, Truy vấn, Đoạn(scheme, netloc, path, query, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
17.

Các lớp sau đây cung cấp việc triển khai kết quả phân tích cú pháp khi hoạt động trên các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64:

Lớp ________ 45 ________ 221 (URL, Đoạn) ¶(url, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
25.

Mới trong phiên bản 3.2.

Lớp ________ 45 ________ 207 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶(scheme, netloc, path, params, query, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
11.

Mới trong phiên bản 3.2.

Lớp ________ 45 ________ 207 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶(scheme, netloc, path, query, fragment)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
11.

Mới trong phiên bản 3.2.

Lớp ________ 45 ________ 207 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
11.

Lớp ________ 45 ________ 213 (Sơ đồ, Netloc, Path, Truy vấn, Đoạn(string, safe='/', encoding=None, errors=None)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
78 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
17.

Các lớp sau đây cung cấp việc triển khai kết quả phân tích cú pháp khi hoạt động trên các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64:

Lớp ________ 45 ________ 221 (URL, Đoạn) ¶Moved from RFC 2396 to RFC 3986 for quoting URL strings. “~” is now included in the set of unreserved characters.

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
25.

Lớp ________ 45 ________ 227 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
31.

Lớp ________ 45 ________ 233 (Sơ đồ, Netloc, Path, Truy vấn, Đoạn(string, safe='', encoding=None, errors=None)

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
37.

Trích dẫn url

Các chức năng trích dẫn URL tập trung vào việc lấy dữ liệu chương trình và làm cho an toàn khi sử dụng làm thành phần URL bằng cách trích dẫn các ký tự đặc biệt và mã hóa một cách thích hợp văn bản không ASCII. Họ cũng hỗ trợ đảo ngược các hoạt động này để tạo lại dữ liệu gốc từ nội dung của thành phần URL nếu tác vụ đó không được đề cập bởi các hàm phân tích cú pháp URL ở trên.(bytes, safe='/')

________ 45 ________ 239 (chuỗi, an toàn = '/', mã hóa = không, lỗi = không) ¶

Thay thế các ký tự đặc biệt trong chuỗi bằng cách sử dụng Escap

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
40. Thư, chữ số và các ký tự
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
41 không bao giờ được trích dẫn. Theo mặc định, hàm này được dự định để trích dẫn phần đường dẫn của URL. Tham số an toàn tùy chọn chỉ định các ký tự ASCII bổ sung không nên được trích dẫn - giá trị mặc định của nó là
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
42.

Chuỗi có thể là đối tượng
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63.(string, encoding='utf-8', errors='replace')

Đã thay đổi trong phiên bản 3.7: Đã chuyển từ RFC 2396 sang RFC 3986 để trích dẫn chuỗi URL. Hiện tại ~ ~ hiện được bao gồm trong tập hợp các ký tự không được bảo vệ.

Các lớp sau đây cung cấp việc triển khai kết quả phân tích cú pháp khi hoạt động trên các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
64:

Lớp ________ 45 ________ 221 (URL, Đoạn) ¶

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
92 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
25.

Lớp ________ 45 ________ 227 (Sơ đồ, Netloc, Path, params, truy vấn, đoạn) ¶string parameter supports bytes and str objects (previously only str).

Lớp cụ thể cho kết quả
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
07 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
31.(string, encoding='utf-8', errors='replace')

Lớp ________ 45 ________ 233 (Sơ đồ, Netloc, Path, Truy vấn, Đoạn

Lớp cụ thể cho kết quả

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
45 có chứa dữ liệu
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63. Phương thức
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
80 trả về một thể hiện
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
37.

Ví dụ:

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
82 mang lại
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
77.

________ 45 ________ 285 (chuỗi) ¶(string)

Thay thế

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
40 thoát ra bằng tương đương một octet của chúng và trả về một đối tượng
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63.

Chuỗi có thể là đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63.

Nếu đó là một

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65, các ký tự không phải ASCII không được phân loại trong chuỗi được mã hóa thành các byte UTF-8.

Ví dụ:

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
91 mang lại
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
92.

________ 45 ________ 294 (Truy vấn, LOSEQ = FALSE, SAFE = ''(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus)

Chuyển đổi một đối tượng ánh xạ hoặc một chuỗi các bộ dữ liệu hai phần tử, có thể chứa các đối tượng

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65 hoặc
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
63, thành chuỗi văn bản ASCII được mã hóa phần trăm. Nếu chuỗi kết quả được sử dụng làm dữ liệu cho hoạt động bài với hàm
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
97, thì nó sẽ được mã hóa thành byte, nếu không nó sẽ dẫn đến
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
74.

Chuỗi kết quả là một loạt các cặp

>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
99 được phân tách bởi
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
00 ký tự, trong đó cả khóa và giá trị được trích dẫn bằng hàm QUOTE_VIA. Theo mặc định,
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
01 được sử dụng để trích dẫn các giá trị, có nghĩa là các khoảng trống được trích dẫn dưới dạng ký tự
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
02 và các ký tự ‘/Hồi được mã hóa dưới dạng
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
03, theo tiêu chuẩn cho các yêu cầu GET (
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
04). Một hàm thay thế có thể được truyền dưới dạng QUOTE_VIA là
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
57, sẽ mã hóa các khoảng trắng dưới dạng
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
06 và không mã hóa các ký tự ‘/. Để kiểm soát tối đa những gì được trích dẫn, sử dụng
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
07 và chỉ định giá trị cho an toàn.

Khi một chuỗi các bộ dữ liệu hai phần tử được sử dụng làm đối số truy vấn, phần tử đầu tiên của mỗi tuple là một khóa và thứ hai là một giá trị. Phần tử giá trị trong chính nó có thể là một chuỗi và trong trường hợp đó, nếu tham số tùy chọn Liều lượng đánh giá là

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
9, các cặp
>>> url = "http://www.example.com/site/section1/VAR1/VAR2"
>>> url.split("/")[-2:]
['VAR1', 'VAR2']
99 riêng lẻ được phân tách bởi
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
00 được tạo cho từng phần tử của chuỗi giá trị cho khóa. Thứ tự của các tham số trong chuỗi được mã hóa sẽ khớp với thứ tự của các bộ dữ liệu tham số trong chuỗi.

Các tham số an toàn, mã hóa và lỗi được truyền lại cho QUOTE_VIA (các tham số mã hóa và lỗi chỉ được truyền khi một phần tử truy vấn là

ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
65).

Để đảo ngược quá trình mã hóa này,

>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
12 và
>>> from urlparse import urlparse
>>> url = 'http://www.example.com/site/section1/VAR1/VAR2' 
>>> urlparse(url)
ParseResult(scheme='http', netloc='www.example.com', path='/site/section1/VAR1/VAR2', params='', query='', fragment='')
>>> urlparse(url).path
'/site/section1/VAR1/VAR2'
>>> urlparse(url).path.split('/')[-2]
'VAR1'
13 được cung cấp trong mô -đun này để phân tích các chuỗi truy vấn vào các cấu trúc dữ liệu Python.

Tham khảo các ví dụ urllib để tìm hiểu làm thế nào phương thức

>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
7 có thể được sử dụng để tạo chuỗi truy vấn của URL hoặc dữ liệu cho yêu cầu POST.urllib examples to find out how the
>>> from urllib.parse import urlparse
>>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
>>> u
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> u._replace(scheme='http')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
7 method can be used for generating the query string of a URL or data for a POST request.

Đã thay đổi trong phiên bản 3.2: Truy vấn hỗ trợ các byte và các đối tượng chuỗi.query supports bytes and string objects.

Mới trong phiên bản 3.5: tham số QUOTE_VIA.quote_via parameter.

Xem thêm

WhatWG - Tiêu chuẩn sống URL

Nhóm làm việc cho tiêu chuẩn URL xác định URL, miền, địa chỉ IP, định dạng Ứng dụng/X-WWW-Form-Urlencoded và API của chúng.

RFC 3986 - Định danh tài nguyên thống nhất - Uniform Resource Identifiers

Đây là tiêu chuẩn hiện tại (STD66). Bất kỳ thay đổi nào đối với mô -đun urllib.parse sẽ phù hợp với điều này. Một số sai lệch có thể được quan sát, chủ yếu là cho mục đích tương thích ngược và đối với các yêu cầu phân tích cú pháp thực tế nhất định như thường thấy trong các trình duyệt chính.

RFC 2732 - Định dạng cho các địa chỉ IPv6 theo nghĩa đen trong URL. - Format for Literal IPv6 Addresses in URL’s.

Điều này chỉ định các yêu cầu phân tích cú pháp của URL IPv6.

RFC 2396 - Số nhận dạng tài nguyên thống nhất (URI): Cú pháp chung - Uniform Resource Identifiers (URI): Generic Syntax

Tài liệu mô tả các yêu cầu cú pháp chung cho cả tên tài nguyên thống nhất (urns) và bộ định vị tài nguyên thống nhất (URL).

RFC 2368 - Sơ đồ URL Mailto. - The mailto URL scheme.

Yêu cầu phân tích cú pháp cho các chương trình URL Mailto.

RFC 1808 - Bộ định vị tài nguyên đồng nhất tương đối - Relative Uniform Resource Locators

Yêu cầu này cho các bình luận bao gồm các quy tắc để tham gia một URL tuyệt đối và tương đối, bao gồm một số lượng lớn các ví dụ bất thường của Hồi giáo điều chỉnh việc xử lý các trường hợp biên giới.

RFC 1738 - Bộ định vị tài nguyên thống nhất (URL) - Uniform Resource Locators (URL)

Điều này chỉ định cú pháp chính thức và ngữ nghĩa của các URL tuyệt đối.

Làm thế nào để bạn chia một liên kết trong Python?

Phương pháp số 1: Sử dụng Split () 'và trả về phần đầu tiên của Split cho kết quả.Using split() ' and return the first part of split for result.

Làm thế nào để bạn phân tích một url chuỗi trong python?

Để phân tích chuỗi truy vấn URL và nhận tham số truy vấn:..
Nhập các phương thức URLPARE và PARSE_QS từ Urllib.Mô -đun phân tích cú pháp ..
Sử dụng phương thức UrlParse để có được một đối tượng kết quả phân tích ..
Chuyển đối tượng cho phương thức pARSE_QS để lấy từ điển của các thông số truy vấn ..

Làm thế nào để bạn làm sạch URL trong Python?

Canonical hóa URL đã cho bằng cách áp dụng các quy trình sau:..
Sắp xếp các đối số truy vấn, đầu tiên theo khóa, sau đó theo giá trị ..
Phần trăm mã hóa đường dẫn và đối số truy vấn.....
Bình thường hóa tất cả các không gian (trong các đối số truy vấn) '+' (biểu tượng cộng).
Bình thường hóa trường hợp mã hóa phần trăm ( %2F -> %2F).

URL chia tay làm gì?

Kiểm tra URL chia giúp bạn: Tạo và chạy nhiều biến thể của trang web của bạn trên các URL khác nhau.Kiểm tra các luồng khác nhau và các thay đổi phức tạp như thiết kế lại hoàn chỉnh trang web của bạn.So sánh các trang web được lưu trữ trên các URL khác nhau.Create and run multiple variations of your website on different URLs. Test different flows and complex changes such as a complete redesign of your website. Compare webpages hosted on different URLs.