Python 提取js中的url

"""
作用:提取js中的url,http开头
用法:python index.py /js
需要输入一个参数,/js是js所在的路径
"""
import os, sys
import re


# 定义查找方法
def Find(string):
    #  查找匹配正则表达式的字符串
    url = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)
    return url
# 读取js方法
def read_js(path):
    print("当前文件是:"+path)
    da = open(path, 'rb').read()
    # print(da)
    try:
        # print(Find(str(da, encoding="utf-8")))
        if len(Find(str(da, encoding="utf-8"))) > 0:
            print(path+":", Find(str(da, encoding="utf-8")))
            return Find(str(da, encoding="utf-8"))
    except UnicodeDecodeError:
        # print(Find(str(da, encoding="gbk")))
        if len(Find(str(da, encoding="gbk"))) > 0:
            print(path + ":", Find(str(da, encoding="gbk")))
            return Find(str(da, encoding="gbk"))

    except:
        return "请尝试其他编码格式"
# 文件递归查询方法
def read_file(path):
    # print(os.listdir(path))
    allarr = []
    for i in os.listdir(path):
        fi_d = os.path.join(path, i)
        if os.path.isdir(fi_d):
            read_file(fi_d)
        else:
            if os.path.splitext(i)[1] == '.js':
                baby = read_js(os.path.join(fi_d))
                if not baby is None and len(baby) > 0:
                    allarr.append(baby)
    
    print(sum(allarr, []))
    exit()

if __name__ == '__main__':
    # 接收路径
    path = sys.argv[1]

    read_file(path)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值