5

This was working with python 2.7 but wont work in 3.5

I am trying to split a url's params then change out one of the params with a new value that is an md5 hash.

The urlbreaks down like this,

ParseResult(scheme='http', netloc='example.com', path='/dynamic', params='', query='t=MD5-YOUR-OAUTH-TOKEN&p=11111111', fragment='')

The new url should look like this,

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)

I get this error,

'function' object has no attribute 'parse'

full stack trace

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'

Is this a problem with my python version?

2 Answers 2

9

Had the same problem this worked for me:

from urllib.parse import urlparse
from urllib.parse import parse_qs

product_url = item.find('product_url').text
parsed = urlparse(product_url)
qs = parse_qs(parsed.query)
Sign up to request clarification or add additional context in comments.

Comments

6

Use from urlparse import urlparse. Or if you want to save compatibility with python 2:

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

UPDATE: I was inattentive. Try to change qs = urlparse.parse.parse_qs(parsed.query) on qs = urllib.parse.parse_qs(parsed.query).

1 Comment

At the top of the page they included it docs.python.org/3.0/library/urllib.parse.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.