我试图在提交之前urlencode这个字符串。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
我试图在提交之前urlencode这个字符串。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
当前回答
另一件可能还没有提到的事情是urllib.urlencode()将字典中的空值编码为字符串None,而不是将该形参作为不存在的参数。我不知道这是否是通常需要的,但不适合我的用例,因此我必须使用quote_plus。
其他回答
试试这个:
urllib.pathname2url(stringToURLEncode)
Urlencode不能工作,因为它只对字典有效。Quote_plus没有产生正确的输出。
供以后参考(例如:For python3)
>>> import urllib.request as req
>>> query = 'eventName=theEvent&eventDescription=testDesc'
>>> req.pathname2url(query)
>>> 'eventName%3DtheEvent%26eventDescription%3DtestDesc'
另一件可能还没有提到的事情是urllib.urlencode()将字典中的空值编码为字符串None,而不是将该形参作为不存在的参数。我不知道这是否是通常需要的,但不适合我的用例,因此我必须使用quote_plus。
如果你不想使用urllib。
https://github.com/wayne931121/Python_URL_Decode
#保留字元的百分號編碼
URL_RFC_3986 = {
"!": "%21", "#": "%23", "$": "%24", "&": "%26", "'": "%27", "(": "%28", ")": "%29", "*": "%2A", "+": "%2B",
",": "%2C", "/": "%2F", ":": "%3A", ";": "%3B", "=": "%3D", "?": "%3F", "@": "%40", "[": "%5B", "]": "%5D",
}
def url_encoder(b):
# https://zh.wikipedia.org/wiki/%E7%99%BE%E5%88%86%E5%8F%B7%E7%BC%96%E7%A0%81
if type(b)==bytes:
b = b.decode(encoding="utf-8") #byte can't insert many utf8 charaters
result = bytearray() #bytearray: rw, bytes: read-only
for i in b:
if i in URL_RFC_3986:
for j in URL_RFC_3986[i]:
result.append(ord(j))
continue
i = bytes(i, encoding="utf-8")
if len(i)==1:
result.append(ord(i))
else:
for c in i:
c = hex(c)[2:].upper()
result.append(ord("%"))
result.append(ord(c[0:1]))
result.append(ord(c[1:2]))
result = result.decode(encoding="ascii")
return result
#print(url_encoder("我好棒==%%0.0:)")) ==> '%E6%88%91%E5%A5%BD%E6%A3%92%3D%3D%%0.0%3A%29'
对于Python 3 urllib3正常工作,您可以根据其官方文档使用以下方法:
import urllib3
http = urllib3.PoolManager()
response = http.request(
'GET',
'https://api.prylabs.net/eth/v1alpha1/beacon/attestations',
fields={ # here fields are the query params
'epoch': 1234,
'pageSize': pageSize
}
)
response = attestations.data.decode('UTF-8')