Hướng dẫn python url encode special characters - python url mã hóa các ký tự đặc biệt

8

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi muốn mã hóa URL với các ký tự đặc biệt. Trong trường hợp của tôi, đó là:

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
2 (nó không phải là một danh sách hữu hạn).

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
3 cho kết quả rất lạ, điều này không chính xác. Làm thế nào khác các biểu tượng này có thể được mã hóa?

Hướng dẫn python url encode special characters - python url mã hóa các ký tự đặc biệt

ALG

14.4K4 Huy hiệu vàng42 Huy hiệu bạc54 Huy hiệu đồng4 gold badges42 silver badges54 bronze badges

Đã hỏi ngày 25 tháng 7 năm 2014 lúc 11:31Jul 25, 2014 at 11:31

5

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
4 cho
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
5

Sử dụng UTF-8 một cách rõ ràng sau đó:

urllib2.quote(u"Grønlandsleiret, Oslo, Norway".encode('UTF-8'))

Và luôn nêu mã hóa trong tệp của bạn. Xem PEP 0263.


Một chuỗi không UTF-8 cần phải được giải mã trước, sau đó được mã hóa:

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.

Đã trả lời ngày 25 tháng 7 năm 2014 lúc 11:39Jul 25, 2014 at 11:39

Kaykaykay

24.8K10 Huy hiệu vàng96 Huy hiệu bạc140 Huy hiệu đồng10 gold badges96 silver badges140 bronze badges

2

Hướng dẫn python url encode special characters - python url mã hóa các ký tự đặc biệt

Mã hóa URL thường là cần thiết khi bạn gọi API từ xa với các chuỗi truy vấn bổ sung hoặc tham số đường dẫn. Bất kỳ chuỗi truy vấn hoặc tham số đường dẫn nào được đặt trong URL phải được mã hóa URL đúng cách.

Mã hóa URL cũng được yêu cầu trong khi chuẩn bị dữ liệu để gửi với loại MIME

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
6.

Trong bài viết này, bạn sẽ học cách mã hóa các thành phần URL trong Python. Bắt đầu nào!

Các chuỗi truy vấn mã hóa URL hoặc các tham số hình thức trong Python (3+)

Trong Python 3+, bạn có thể mã hóa URL bất kỳ chuỗi nào bằng hàm

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 được cung cấp bởi gói
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
8. Hàm
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 theo mặc định sử dụng sơ đồ mã hóa
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
0.

Hãy để xem một ví dụ -

>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'

Lưu ý rằng, chức năng

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 xem xét an toàn nhân vật ____22 theo mặc định. Điều đó có nghĩa là, nó không mã hóa ký tự
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
2 -
-

>> urllib.parse.quote('/')
'/'

Hàm

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 chấp nhận một tham số được đặt tên có tên là Safe có giá trị mặc định là
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
2. Nếu bạn cũng muốn mã hóa ký tự
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
2, thì bạn có thể làm như vậy bằng cách cung cấp một chuỗi trống trong tham số an toàn như thế này-

>>> urllib.parse.quote('/', safe='')
'%2F'

Mã hóa các ký tự không gian thành dấu cộng (>>> import urllib.parse >>> query = 'Hellö Wörld@Python' >>> urllib.parse.quote(query) 'Hell%C3%B6%20W%C3%B6rld%40Python'7) sử dụng hàm >>> import urllib.parse >>> query = 'Hellö Wörld@Python' >>> urllib.parse.quote(query) 'Hell%C3%B6%20W%C3%B6rld%40Python'8

Hàm

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 mã hóa các ký tự không gian thành
>> urllib.parse.quote('/')
'/'
0. Nếu bạn muốn mã hóa các ký tự không gian thành dấu cộng (
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
7), thì bạn có thể sử dụng một chức năng khác có tên
>> urllib.parse.quote('/')
'/'
2 được cung cấp bởi gói
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
8.

>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote_plus(query)
'Hell%C3%B6+W%C3%B6rld%40Python'

Đọc: Khi nào nên mã hóa không gian thành Plus (+) hoặc %20? When to encode space to plus (+) or %20?

Mã hóa nhiều tham số cùng một lúc

Bạn có thể mã hóa nhiều tham số cùng một lúc bằng hàm

>> urllib.parse.quote('/')
'/'
4. Đây là một hàm tiện lợi có một từ điển của các cặp giá trị chính hoặc một chuỗi các bộ dữ liệu hai phần tử và sử dụng hàm
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
8 để mã hóa mọi giá trị. Chuỗi kết quả là một loạt các cặp
>> urllib.parse.quote('/')
'/'
6 được phân tách bằng ký tự
>> urllib.parse.quote('/')
'/'
7.

Hãy để xem một ví dụ -

>>> import urllib.parse
>>> params = {'q': 'Python URL encoding', 'as_sitesearch': 'www.urlencoder.io'}
>>> urllib.parse.urlencode(params)
'q=Python+URL+encoding&as_sitesearch=www.urlencoder.io'

Nếu bạn muốn chức năng

>> urllib.parse.quote('/')
'/'
8 sử dụng hàm
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7 cho các tham số mã hóa, thì bạn có thể làm như vậy như thế này -

urllib.parse.urlencode(params, quote_via=urllib.parse.quote)

Mã hóa nhiều tham số cùng một lúc trong đó một tham số có thể có nhiều giá trị

Hàm

>> urllib.parse.quote('/')
'/'
8 có một đối số tùy chọn gọi là
>>> urllib.parse.quote('/', safe='')
'%2F'
1. Nếu đầu vào của bạn có thể có nhiều giá trị cho một khóa, thì bạn nên đặt đối số
>>> urllib.parse.quote('/', safe='')
'%2F'
1 thành
>>> urllib.parse.quote('/', safe='')
'%2F'
3 để tất cả các giá trị được mã hóa đúng -

>>> import urllib.parse
>>> params = {'name': 'Rajeev Singh', 'phone': ['+919999999999', '+628888888888']}
>>> urllib.parse.urlencode(params, doseq=True)
'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'

Mã hóa URL trong Python 2.x

Trong Python 2.x, các chức năng

                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
7,
>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'
8 và
>> urllib.parse.quote('/')
'/'
8 có thể được truy cập trực tiếp từ gói
>>> urllib.parse.quote('/', safe='')
'%2F'
7. Các chức năng này đã được tái cấu trúc thành gói
                           # You've got a str "s".
s = s.decode('latin-1')    # (or what the encoding might be …)
                           # Now "s" is a unicode object.
s = s.encode('utf-8')      # Encode as UTF-8 string.
                           # Now "s" is a str again.
s = urllib2.quote(s)       # URL encode.
                           # Now "s" is encoded the way you need it.
8 trong Python 3.

Các ví dụ sau đây cho thấy cách bạn có thể thực hiện mã hóa URL trong Python 2.x bằng cách sử dụng các chức năng trên.

  1. urllib.quote()

    >>> import urllib
    >>> urllib.quote('Hello World@Python2')
    'Hello%20World%40Python2'

  2. urllib.quote_plus (): mã hóa không gian thành dấu cộng (xông+))

                               # You've got a str "s".
    s = s.decode('latin-1')    # (or what the encoding might be …)
                               # Now "s" is a unicode object.
    s = s.encode('utf-8')      # Encode as UTF-8 string.
                               # Now "s" is a str again.
    s = urllib2.quote(s)       # URL encode.
                               # Now "s" is encoded the way you need it.
    
    0

  3. urllib.urlencode (): mã hóa nhiều tham số

                               # You've got a str "s".
    s = s.decode('latin-1')    # (or what the encoding might be …)
                               # Now "s" is a unicode object.
    s = s.encode('utf-8')      # Encode as UTF-8 string.
                               # Now "s" is a str again.
    s = urllib2.quote(s)       # URL encode.
                               # Now "s" is encoded the way you need it.
    
    1

Cũng đọc: Cách giải mã các thành phần URL trong Python

Người giới thiệu

  • Python urllib.parse.quote ()
  • Python urllib.parse.quote_plus ()
  • Khi nào nên mã hóa không gian thành Plus (+) hoặc %20?

Làm cách nào để mã hóa một url trong Python?

Mã hóa URL trong Python 2 ...
urllib.quote () >>> nhập urllib >>> urllib. ....
urllib.quote_plus (): mã hóa không gian thành dấu cộng ('+') >>> nhập urllib >>> urllib. ....
urllib.urlencode (): mã hóa nhiều tham số. >>> nhập urllib >>> params = {'q': 'mã hóa url python 2.x', 'as_sitearch': 'www.urlencoder.io'} >>> urllib ..

Làm thế nào để bạn mã hóa và giải mã một URL trong Python?

Giải mã URL Python..
Sử dụng hàm urllib.parse.unquote () để giải mã URL trong Python ..
Sử dụng hàm urllib.parse.unquote_plus () để giải mã URL trong Python ..
Sử dụng mô -đun yêu cầu để giải mã URL trong Python ..

Python có yêu cầu url mã hóa không?

Phương thức mã hóa URL yêu cầu trong gói python parse phải được sử dụng để url mã hóa bất kỳ chuỗi nào để giải quyết vấn đề này.Hệ thống mã hóa UTF-8 là mặc định cho hàm báo giá ().Hãy nhớ rằng hàm báo giá () theo mặc định coi / ký tự là an toàn.Nó không mã hóa hoặc đặc trưng.parse package must be used to URL encode any string in order to resolve this problem. The UTF-8 encoding system is the default for the quote() function. Keep in mind that the quote() function by default deems the / character to be safe. It doesn't encode or characterize.

Url url url accode làm gì?

Sử dụng hàm urllib.parse.urlencode () để 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.