gpt4 book ai didi

python - 使用 Python URLParse Parse_qs 解析 URL

转载 作者:行者123 更新时间:2023-11-28 21:47:38 24 4
gpt4 key购买 nike

这适用于 python 2.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 版本的问题吗?

最佳答案

有同样的问题,这对我有用:

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)

关于python - 使用 Python URLParse Parse_qs 解析 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36298951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com