简体   繁体   中英

Parsing A URL With Python URLParse Parse_qs

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?

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) .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM