我是Python的新手,似乎遇到了一个问题。我正在尝试对用户代理字符串进行URL编码...
import urllib
UserAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3'
print 'Agent: ' + UserAgent
print urllib.urlencode(UserAgent)
这导致...
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3
Traceback (most recent call last):
File "D:\Source\SomePath\test.py", line 7, in <module>
print urllib.urlencode(UserAgent)
File "C:\Python26\lib\urllib.py", line 1254, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object
Press any key to continue . . .
我只能假设虽然 UserAgent
被正确打印,但我可能在输入时缺少某些字符串转义选项或者在使用 urllib.urlencode()
时犯了一些基本错误?