这在Python2.7中有效,但在3.5中无效
我正在尝试分割一个url的参数,然后用一个新值(md5散列)更改其中一个参数。
URL就这样崩溃了ParseResult(scheme='http', netloc='example.com', path='/dynamic', params='', query='t=MD5-YOUR-OAUTH-TOKEN&p=11111111', fragment='')
新的url应该是这样的newString = 'http://example.com/dynamic?t='+tokenHashed+'&p=11112311312'
import requests, json, hashlib
import urllib
from urllib.parse import urlparse
from xml.etree import ElementTree
product_url = item.find('product_url').text
parsed = urlparse(product_url)
qs = urlparse.parse.parse_qs(parsed.query)
qs['t'] = [tokenHashed]
newqs = urllib.urlencode(qs, doseq=1)
newurl = urlparse.urlunparse([newqs if i == 4 else x for i,x in enumerate(parsed)])
print(newurl)
我知道这个错误'function' object has no attribute 'parse'
全堆栈跟踪Environment:
Request Method: POST
Request URL: http://localhost:8000/serviceapp/example/
Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tastypie',
'haystack',
'serviceapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Applications/AMPPS/www/djang-rest/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Applications/AMPPS/www/djang-rest/restapi/serviceapp/views.py" in example
20. getProdcuts(advertiserName)
File "/Applications/AMPPS/www/djang-rest/restapi/serviceapp/views.py" in getProdcuts
42. qs = urlparse.parse.parse_qs(parsed.query)
Exception Type: AttributeError at /serviceapp/example/
Exception Value: 'function' object has no attribute 'parse'
我的python版本有问题吗?