python url转码Python3中的urlencode和urldecode AttributeError: module urllib has no attribute parse

Python3中的urlencode和urldecode

AttributeError: module ‘urllib’ has no attribute ‘parse

代码目的是将“python url转码” 转换为urlcode

(venv) leenhem@DESKTOP-0ACGT17:~$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> uri='https://www.baidu.com/s?ct=2097152&si=www.code404.icu&wd='
>>> keywords='python url转码'
>>> encode_keywords=urllib.parse.quote_plus(keywords)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'parse'
报错: Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'urllib' has no attribute 'parse'

意思就是找不到parse类

解决方法:引入方式不对
正确引入方式:import urllib.parse

(venv) leenhem@DESKTOP-0ACGT17:~$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.parse
>>> uri='https://www.baidu.com/s?ct=2097152&si=www.code404.icu&wd='
>>> keywords='python url转码'
>>> encode_keywords=urllib.parse.quote_plus(keywords)
>>> url=uri+encode_keywords
>>> print(url)
https://www.baidu.com/s?ct=2097152&si=www.code404.icu&wd=python+url%E8%BD%AC%E7%A0%81
>>>

已上输出已转码为urlcode,觉得有用就给个赞吧

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/231.html

发表评论

登录后才能评论