トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Python URLエンコード



目次



記事一覧

キーワード

Python URLエンコード

[Python][文字化け]


 urllib

urllib.quote

構文
quote(s, safe='/')

予約文字
reserved    = gen-delims / sub-delims
gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="
非予約文字
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
区切り文字
delims      = "<" | ">" | "#" | "%" | <">
安全でない
unwise      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

例1
>>> import urllib
>>> urllib.quote('abc def')
'abc%20def'
例2
>>> urllib.quote('http://test.com?p=abc def')
'http%3A//test.com%3Fp%3Dabc%20def'
例3
>>> urllib.quote('http://test.com?p=abc def', safe=';/?:@&=+$,')
'http://test.com?p=abc%20def'

urllib.urlencode

  • タプル、またはディクショナリを、URL クエリー文字列として生成する
>>> import urllib
>>> param = {'q':'test', 'id':'piroto'}
>>> urllib.urlencode(param)
'q=test&id=piroto'

HTML特殊文字のエスケープ、アンエスケープ

  • xml.sax.saxutils の escape, unescape を利用すると便利
>>> from xml.sax.saxutils import *
>>> escape("<&>")
'&lt;&amp;&gt;'
>>> unescape("&lt;&amp;&gt;")
'<&>'
>>> unescape("'",{"'":"'"})
"'"



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.