python-短网址批量还原
生活随笔
收集整理的這篇文章主要介紹了
python-短网址批量还原
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python-短網(wǎng)址批量還原源碼
#-*- coding = utf-8 -*- #Time : 2021/1/23 #Author : 小檸檬 #File : 短網(wǎng)址.py import gevent.pool from gevent import monkey monkey.patch_all() import requests import json import os import re from urllib.parse import unquote import timedef makeFile():if not os.path.exists(r'C:\Users\Administrator\Desktop\短網(wǎng)址'):os.makedirs(r'C:\Users\Administrator\Desktop\短網(wǎng)址')with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\短網(wǎng)址.txt','a') as f:f.write('')with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\原網(wǎng)址.txt','a') as f:f.write('')with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\還原失敗網(wǎng)址.txt','a') as f:f.write('')def emptyTxt():with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\原網(wǎng)址.txt', 'w') as f:f.write('')with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\還原失敗網(wǎng)址.txt', 'w') as f:f.write('')def restoreUrl(shortUrl):url = 'http://api.dwzjh.com/api/reduction?url={}'.format(shortUrl)try:r = requests.get(url)if json.loads(r.text).get('code') == 1:regex = re.compile(r'qc=(.*)"')longUrl = re.findall(regex,r.text)[0]print(unquote(longUrl))with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\原網(wǎng)址.txt', 'a') as f:f.write(unquote(longUrl)+'\n')else:print(shortUrl,json.loads(r.text).get('msg'))with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\還原失敗網(wǎng)址.txt', 'a') as f:f.write(unquote(shortUrl)+'\n')except Exception as e:print(e)def main():with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\短網(wǎng)址.txt', 'r') as f:shortUrlList = f.readlines()shortUrlList = [short.strip() for short in shortUrlList if short != '\n']print('短網(wǎng)址數(shù)量:',len(shortUrlList))with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\短網(wǎng)址.txt', 'w') as f:f.write('')with open(r'C:\Users\Administrator\Desktop\短網(wǎng)址\短網(wǎng)址.txt', 'a') as f:for short in shortUrlList:f.write(short+'\n')pool = gevent.pool.Pool(10)pool.map(restoreUrl,shortUrlList)print('耗時(shí):{}'.format(time.perf_counter()),'還原完成啦~')input('按下回車(chē)鍵結(jié)束程序...')if __name__ == '__main__':makeFile()emptyTxt()main()總結(jié)
以上是生活随笔為你收集整理的python-短网址批量还原的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WinMap+ | 稳马企业金服基于C4
- 下一篇: ⚡自组织映射(SOM)神经网络⚡Pyth