复制网址,粘贴带标题

2023-09-18  本文已影响0人  陈成_Adam
import clipboard
import requests
import re


def is_url(string):
    url_pattern = re.compile(r'^(http|https)://[^\s/$.?#].[^\s]*$')
    return bool(re.match(url_pattern, string))


if __name__ == '__main__':
    if is_url(clipboard.paste()):
        url = clipboard.paste()
        response = requests.get(url)

        title_matched = re.match(r'.*<title.*?>(.*?)</title>.*', response.text, re.DOTALL)
        if title_matched:
            title = title_matched.group(1).strip()

            out_str = '%s[%s]' % (url, title)
            print(out_str)
            clipboard.copy(out_str)
        else:
            print('没有找到标题,网址是:%s' % url)
    else:
        print('复制的不是一个网址')
上一篇下一篇

猜你喜欢

热点阅读