无法在Python 3中使用amazonify。ModuleNotFoundError:没有名为“urlparse”的模块

2024-06-16 13:58:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码:

from amazonify import amazonify
from urllib.parse import urlparse

User_url = input('Enter the link here: ')

我得到的错误是:

File "C:\Users\jashandeep\AppData\Local\Programs\Python\Python39\lib\site-packages\amazonify.py", line 4, in
from urlparse import urlparse, urlunparse ModuleNotFoundError: No module named 'urlparse'


Tags: the代码fromimporturlinputhereparse
1条回答
网友
1楼 · 发布于 2024-06-16 13:58:33

您需要update this import来添加Python 3支持:

发件人:

from urlparse import urlparse, urlunparse, parse_qs
from urllib import urlencode

致:

from urllib.parse import urlparse, urlunparse, parse_qs, urlencode

您的选择:

  1. 将此包与Python2一起使用

  1. 更新包源代码

修改amazonify.py中的导入语句

在某些情况下,更改源代码可能会产生意外的后果(例如,对于依赖包和脚本)。一般来说,我不建议这样做。但是这个软件包看起来很简单,而且很长时间没有更新

看来there will be no update。你需要自己做每件事


  1. 将其用作独立脚本

这个包基本上是一个Python函数。软件包的作者没有指定许可证,但是您可以编写与本例类似的脚本

from urllib.parse import urlparse, urlunparse, parse_qs, urlencode


def amazonify(url, affiliate_tag):
    """Generate an Amazon affiliate link given any Amazon link and affiliate
    tag.

    :param str url: The Amazon URL.
    :param str affiliate_tag: Your unique Amazon affiliate tag.
    :rtype: str or None
    :returns: An equivalent Amazon URL with the desired affiliate tag included,
        or None if the URL is invalid.
    """
    # Ensure the URL we're getting is valid:
    new_url = urlparse(url)
    if not new_url.netloc:
        return None
    # Add or replace the original affiliate tag with our affiliate tag in the
    # querystring. Leave everything else unchanged.
    query_dict = parse_qs(new_url[4])
    query_dict['tag'] = affiliate_tag
    new_url = new_url[:4] + (urlencode(query_dict, True), ) + new_url[5:]

    return urlunparse(new_url)


if __name__ == '__main__':
    
    affiliate_tag = 'tag'

    urls = [...]
    affiliate_urls = [amazonify(u, affiliate_tag) for u in urls]
    print(affiliate_urls)

相关问题 更多 >