Hi All

When trying to open a URL with bellow code, it Gives "TypeError: not all arguments converted during string formatting"

import urllib

City="%D9%85%D9%87%D8%B1%D8%A7%D9%86"
Province="%D8%A7%DB%8C%D9%84%D8%A7%D9%85"
myurl = 'http://www.owghat.com/owghat.png.aspx?Province='+'%s'+'&City='+'%s'%(Province, City)
#the real address is:http://www.owghat.com/owghat.png.aspx?Province=%D8%A7%DB%8C%D9%84%D8%A7%D9%85&City=%D9%85%D9%87%D8%B1%D8%A7%D9%86

a="e:\\img.png"
urllib.urlretrieve(myurl,a)

So, what I'm doing wrong?

AND Another question: How to encode a utf-8 string(example: مهران) to something like "%D9%85%D9%87%D8%B1%D8%A7%D9%86" in a url?

Thanks in advance for any help.

Edited 5 Years Ago by M.S.: n/a

You are having unnecessary closing of quotes and plusses:

import urllib

City="%D9%85%D9%87%D8%B1%D8%A7%D9%86"
Province="%D8%A7%DB%8C%D9%84%D8%A7%D9%85"
myurl = 'http://www.owghat.com/owghat.png.aspx?Province=%s&City=%s'%(Province, City)
#the real address is:http://www.owghat.com/owghat.png.aspx?Province=%D8%A7%DB%8C%D9%84%D8%A7%D9%85&City=%D9%85%D9%87%D8%B1%D8%A7%D9%86

a="e:\\img.png"
urllib.urlretrieve(myurl,a)

Your string seems not valid for utf-8:

>>> u'مهران'.encode('utf-8')
Unsupported characters in input

Otherwise you could do like this:

>>> c = u'Hölmö hämäläinen'.encode('utf-8')
>>> urllib.quote(c)
'H%C3%B6lm%C3%B6%20h%C3%A4m%C3%A4l%C3%A4inen'
>>>

Edited 5 Years Ago by pyTony: n/a

thank you dear pyTony.
First problem solved:D
And yes you are right about that string. but the string is Persian(farsi) and i think its utf-8. please tell me if I'm wrong.
but I dont know how they encode urls like this.
in IE the adress is:

http://www.owghat.com/owghat.png.aspx?Province=ایلام&City=مهران

and in FireFox it is:

http://www.owghat.com/owghat.png.aspx?Province=%D8%A7%DB%8C%D9%84%D8%A7%D9%85&City=%D9%85%D9%87%D8%B1%D8%A7%D9%86

I need to download 300 images for 300 cities in 30 provinces and its a damn huge job.

the problem is that with this code, it allways saves the Site's default png image( here for Province: Tehran and City: Tehran) no matter which province or city we use in urlopen method.
if we use for example:

url = 'http://www.owghat.com/owghat.png.aspx?Province=کرمان&City=جیرفت'

it again saves the default png for tehran(Province=تهران&City=تهران).
it means that it actually doesnt open the url we use with Farsi city or province names.

Edited 5 Years Ago by M.S.: n/a

I think that the problem is in the site itself.
If I can find out how they encode Farsi strings to codes like this: %D8%A7%DB%8C%D9%84%D8%A7%D9%85
then the problem will solve easily, i think.
this encoding seems like Hexadecimal, but i faild to encode Farsi strings to valid hex codes

Edited 5 Years Ago by M.S.: n/a

>>> print(unicode(urllib2.unquote('%D8%A7%DB%8C%D9%84%D8%A7%D9%85'), 'utf8'))
ایلام
>>> print(u'ایلام'.encode('utf8'))
Unsupported characters in input

>>>

Edited 5 Years Ago by pyTony: n/a

>>> print(unicode(urllib2.unquote('%D8%A7%DB%8C%D9%84%D8%A7%D9%85'), 'utf8'))
ایلام
>>> print(u'ایلام'.encode('utf8'))
Unsupported characters in input

>>>

On my computer

>>> import urllib2
>>> urllib2.quote('ایلام')
'%D8%A7%DB%8C%D9%84%D8%A7%D9%85'

Thank you guys
in python s60 1.45, python 2.2 and python 2.5.1 it gives error,
anyway thank this great community. I will mark this thread as solved, because my main problem in first post is solved.

This question has already been answered. Start a new discussion instead.