urlsplit/urlunsplit-url拆分和组合方法

def _update_url_query(url, **kwargs):
        """更新URL的参数部分, 保留原有其他参数的值.
            url = "https://www.google.com/search?q=php是世界上最好的语言"
            self._update_url_query(url, q="python是世界上最好的语言")
            返回 "https://www.google.com/search?q=python是世界上最好的语言"

        Args:
            url (str): 原url
            kwargs: 传入需要更新的参数名和参数值

        Returns:
            str: 返回新的url
        """
        from urllib.parse import (
            urlsplit,
            urlunsplit,
            SplitResult,
            parse_qs,
            urlencode,
        )

        res = urlsplit(url)
        query = parse_qs(res.query)
        query = {k: v[0] for k, v in query.items()}
        query.update(kwargs)
        print(res[4:])
        return urlunsplit(SplitResult(*res[:3], urlencode(query), *res[4:]))
url="https://qnmlgb.tech/hot?code=a4e3259e6af67f2b4619b8d7b1e6eae7"
last_article="5e369ced667c7a6bed6e1933"
next_page =_update_url_query(url, _lst=last_article)
print(next_page)

你可能感兴趣的:(urlsplit/urlunsplit-url拆分和组合方法)