在 GAE 上 fetch 有中文路徑的網址
普通在 Google App Engine (GAE) 上要 fetch 網址用
urlfetch.fetch("http://www.test.com/")
就可以了~
如果有中文網址的話怎麼辦呢?
urllib.quote("http://www.test.com/中文路徑/test.htm")
結果是:
http%3A//www.test.com/%E4%B8%AD%E6%96%87%E8%B7%AF%E5%BE%91/test.htm
但是這樣用 urlfetch.fetch 會出現錯誤 InvalidURLError: ApplicationError: 1
解決方法是加上 safe 參數,
讓 ":/?&=" 這五種符號不要被編碼成url編碼
urllib.quote("http://www.test.com/中文路徑/test.htm", safe=":/?&=")
結果是:
http://www.test.com/%E4%B8%AD%E6%96%87%E8%B7%AF%E5%BE%91/test.htm
這樣就可以順利使用 urlfetch.fetch 了~
繼續閱讀!