Python url_escape

escape.py

# -*- coding: utf8 -*-
import sys

if type('') is not type(b''):
    def u(s):
        return s
    bytes_type = bytes
    unicode_type = str
    basestring_type = str
else:
    def u(s):
        return s.decode('unicode_escape')
    bytes_type = str
    unicode_type = unicode
    basestring_type = basestring


try:
    import urllib.parse as urllib_parse  # py3
except ImportError:
    import urllib as urllib_parse  # py2



def url_escape(value):
    """Returns a URL-encoded version of the given value."""
    return urllib_parse.quote_plus(utf8(value))

# python 3 changed things around enough that we need two separate
# implementations of url_unescape.  We also need our own implementation
# of parse_qs since python 3's version insists on decoding everything.
if sys.version_info[0] < 3:
    def url_unescape(value, encoding='utf-8'):
        """Decodes the given value from a URL.

        The argument may be either a byte or unicode string.

        If encoding is None, the result will be a byte string.  Otherwise,
        the result is a unicode string in the specified encoding.
        """
        if encoding is None:
            return urllib_parse.unquote_plus(utf8(value))
        else:
            return unicode_type(urllib_parse.unquote_plus(utf8(value)), encoding)

else:
    def url_unescape(value, encoding='utf-8'):
        """Decodes the given value from a URL.

        The argument may be either a byte or unicode string.

        If encoding is None, the result will be a byte string.  Otherwise,
        the result is a unicode string in the specified encoding.
        """
        if encoding is None:
            return urllib_parse.unquote_to_bytes(value)
        else:
            return urllib_parse.unquote_plus(to_basestring(value), encoding=encoding)

    def parse_qs_bytes(qs, keep_blank_values=False, strict_parsing=False):
        """Parses a query string like urlparse.parse_qs, but returns the
        values as byte strings.

        Keys still become type str (interpreted as latin1 in python3!)
        because it's too painful to keep them as byte strings in
        python3 and in practice they're nearly always ascii anyway.
        """
        # This is gross, but python3 doesn't give us another way.
        # Latin1 is the universal donor of character encodings.
        result = _parse_qs(qs, keep_blank_values, strict_parsing,
                           encoding='latin1', errors='strict')
        encoded = {}
        for k, v in result.items():
            encoded[k] = [i.encode('latin1') for i in v]
        return encoded


_UTF8_TYPES = (bytes_type, type(None))


def utf8(value):
    """Converts a string argument to a byte string.

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    """
    if isinstance(value, _UTF8_TYPES):
        return value
    assert isinstance(value, unicode_type)
    return value.encode("utf-8")

_TO_UNICODE_TYPES = (unicode_type, type(None))


def to_unicode(value):
    """Converts a string argument to a unicode string.

    If the argument is already a unicode string or None, it is returned
    unchanged.  Otherwise it must be a byte string and is decoded as utf8.
    """
    if isinstance(value, _TO_UNICODE_TYPES):
        return value
    assert isinstance(value, bytes_type)
    return value.decode("utf-8")


if __name__ == '__main__':
    args = sys.argv[1:]
    if len(args) != 2:
        sys.exit(1)

    if args[0] == 'encode':
        print utf8(url_escape(args[1]))
        sys.exit(0)

    if args[0] == 'decode':
        print utf8(url_unescape(args[1]))
        sys.exit(0)

    

 

转载于:https://www.cnblogs.com/gibbon/p/Python.html

以下是Python抖音视频去水印的方法: ```python import requests import re import json import os import sys import time import tkinter as tk from tkinter import filedialog def get_video_url(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} res = requests.get(url, headers=headers) if res.status_code == 200: playwm_url = re.findall(r'playAddr: "(.*?)"', res.text) if playwm_url: video_url = playwm_url[0].encode('utf-8').decode('unicode_escape') return video_url def download_video(url, file_path): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} res = requests.get(url, headers=headers, stream=True) total_size = int(res.headers['Content-Length']) if res.status_code == 200: label.config(text='文件大小:' + str(round(total_size / 1024 / 1024, 2)) + 'MB') with open(file_path, 'wb') as f: for chunk in res.iter_content(chunk_size=1024): f.write(chunk) f.flush() progress['value'] += len(chunk) root.update() else: label.config(text='下载失败') def get_video_info(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} res = requests.get(url, headers=headers) if res.status_code == 200: video_info = re.findall(r'<script>tac=(.*?)</script>', res.text) if video_info: tac = video_info[0] video_info_url = 'http://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + re.findall(r'itemId: "(.*?)"', tac)[0] res = requests.get(video_info_url, headers=headers) if res.status_code == 200: video_info = json.loads(res.text) return video_info['item_list'][0]['desc'] def download(): url = entry.get() video_url = get_video_url(url) if video_url: file_name = get_video_info(url) + '.mp4' file_path = os.path.join(path, file_name) download_video(video_url, file_path) label.config(text='下载完成') else: label.config(text='解析失败') def select_path(): global path path = filedialog.askdirectory() path_label.config(text=path) root = tk.Tk() root.title('抖音去水印') root.geometry('400x200') label = tk.Label(root, text='请输入抖音视频链接', font=('微软雅黑', 12)) label.pack(pady=10) entry = tk.Entry(root, font=('微软雅黑', 12)) entry.pack(pady=10) button = tk.Button(root, text='下载', font=('微软雅黑', 12), command=download) button.pack(pady=10) path_button = tk.Button(root, text='选择保存路径', font=('微软雅黑', 12), command=select_path) path_button.pack(pady=10) path_label = tk.Label(root, text='请选择保存路径', font=('微软雅黑', 12)) path_label.pack(pady=10) progress = tk.ttk.Progressbar(root, orient='horizontal', length=200, mode='determinate') progress.pack(pady=10) root.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值