找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 70|回复: 0

python爬虫headers处理小工具源码优化(支持浏览器、Charles、Fillder)

[复制链接]

2万

主题

134

回帖

13万

积分

管理员

积分
134018
发表于 2024-6-7 08:48:48 | 显示全部楼层 |阅读模式 IP:山东省济南市 联通

登录后更精彩...O(∩_∩)O...

您需要 登录 才可以下载或查看,没有账号?立即注册

×
python爬虫headers处理小工具源码优化(支持浏览器、Charles、Fillder)



基于之前的源代码进行优化,(支持浏览器、Charles、Fillderhttps://www.52pojie.cn/thread-1144758-1-1.html







[Python] 纯文本查看 复制代码
import tkinter as tk
import tkinter.messagebox

class MyApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('header处理工具')
        win_width = self.winfo_screenwidth()
        win_height = self.winfo_screenheight()
        my_width = 800
        my_height = 560
        x = (win_width - my_width) / 2
        y = (win_height - my_height) / 2
        self.geometry("%dx%d+%d+%d" % (my_width, my_height, x, y))
        self.maxsize(my_width, my_height)
        self.minsize(my_width, my_height)
        self.button = tk.Button(self, text='转换', font=('宋体', 12), command=self.update)
        self.button.place(x=380, y=300)
        self.label1 = tk.Label(self, text='原始文本', font=('宋体', 10))
        self.label1.place(x=160, y=5)
        self.text1 = tk.Text(width=50, height=40)
        self.text1.place(x=5, y=30)
        self.label2 = tk.Label(self, text='结果文本', font=('宋体', 10))
        self.label2.place(x=600, y=5)
        self.text2 = tk.Text(width=50, height=40)
        self.text2.place(x=440, y=30)

    def update(self):
        if len(self.text1.get('0.0', 'end')) == 1:
            tkinter.messagebox.showinfo('提示', '请先填入内容继续')
        else:
            self.text2.delete('1.0', 'end')
            text = self.text1.get('0.0', 'end').split("\n")
            self.text2.insert('insert', "headers = {\n")
            text = [i for i in text if (i is not None) and (str(i).strip() != '')]
            for _ in text:
                value = ''
                if '\t' in _:
                    key_, value_ = _.split('\t', 1)
                    value = f"'{key_}': '{value_}'"
                elif ':' in _ and _[0] != ':' and '\t' not in _:
                    key_, value_ = _.split(':', 1)
                    value = f"'{key_}': '{value_.replace(' ', '', 1)}'"
                elif ':' in _ and _[0] == ':' and '\t' not in _:
                    if _[0] == ':':
                        key_, value_ = _.replace(':', '', 1).split(':', 1)
                    else:
                        key_, value_ = _.replace(':', '', 1).split(':', 1)
                    value = f"'{key_}': '{value_.replace(' ', '', 1)}'"
                if text.index(_) < len(text) - 1 and _ != '' and _ != '\n':
                    value = value + ',\n'
                self.text2.insert('insert', value)
            self.text2.insert('insert', "\n}")
            self.text2.update()

def main():
    app = MyApp()
    app.mainloop()

if __name__ == '__main__':
    main()


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|时间戳|加密|CTF WiKi|CTF平台汇总|CTF show|ctfhub|棱角安全|rutracker|攻防世界|php手册|peiqi文库|CyberChef|猫捉鱼铃|手机版|小黑屋|cn-sec|IOTsec-Zone|在线工具|分享屋 ( 鲁ICP备2021028754号 )

GMT+8, 2024-9-8 11:15

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表