python 爬虫需知

📅 2026/8/6 11:08:27
python 爬虫需知
Python常见的爬虫命令 实例 import requests # 目标网页 url https://www.xxx.com headers {User-Agent:Mozilla/5.0} # 发起请求 resp requests.get(url, headersheaders) resp.encoding utf-8 # 打印网页源码 print(resp.text) # 提取网页标题 title re.search(rtitle(.*?)/title, resp.text).group(1) print(页面标题, title) 1.python的五大模块 1网络请求 requests爬虫、发包、POC 必备 1基础用法 import requests headers { User-Agent: Mozilla/5.0, Cookie: xxxxxx } # GET 请求 res requests.get(http://xxx.com?id1, headersheaders, timeout3) # POST 表单提交 res requests.post(http://xxx.com/login, data{user:admin,pwd:123456}, headersheaders) # POST JSON接口 res requests.post(http://xxx.com/api, json{username:admin}, headersheaders) res.text # 网页源码字符串 res.content # 二进制内容下载图片、流量包 res.status_code # 状态码 200正常 404不存在 500服务器错误 res.cookies # 获取响应Cookie 2对接 Burp 抓包渗透调试神器 proxies { http: http://127.0.0.1:8080, https: http://127.0.0.1:8080 } # 所有流量走Burp代理 requests.get(url, proxiesproxies, verifyFalse) 3会话保持维持登录状态 session requests.Session() session.post(登录地址, data{user:admin,pass:123}) res session.get(后台页面) # 自动带上登录cookie 2线程并发 IO 密集场景发包、ping、端口扫描全部用 ThreadPoolExecutor 1基础模版 from concurrent.futures import ThreadPoolExecutor def task(num): print(f执行任务{num}) # 开启50个线程并发 with ThreadPoolExecutor(max_workers50) as pool: for n in range(1, 101): pool.submit(task, n) 2批量检测多个网站是否存在 SQL 注入 import requests from concurrent.futures import ThreadPoolExecutor def check_sql(url): payload /?id1 try: r requests.get(urlpayload, timeout2) if MySQL syntax in r.text: print(f存在注入{url}) except: pass urls [http://a.com,http://b.com,http://c.com] with ThreadPoolExecutor(20) as pool: pool.map(check_sql, urls) 3文件读写日志分析、保存扫描结果、读取字典 推荐 with open() 写法自动关闭文件不会造成文件占用 1写入文本 # w 清空写入a 追加写入r 只读 with open(result.txt, a, encodingutf-8) as f: f.write(存活IP192.168.1.100\n) 2读取字典 pwd_list [] with open(password.txt, r, encodingutf-8) as f: for line in f: pwd line.strip() # 去掉换行空格 pwd_list.append(pwd) 3二进制读写读pcap图片 # rb 二进制读wb二进制写 with open(test.png, rb) as f: data f.read() 4正则re提取 flag、手机号、URL、敏感字段渗透高频 · re.search()匹配单个目标找到就返回 · re.findall()批量匹配所有结果返回列表 · re.S让 . 可以匹配换行符 eg案例 import re html flag{abc123xyz} 手机号13800138000 13900139000 http://xxx.com/admin.php # 提取flag flag re.search(rflag\{.*?\}, html, re.S).group() print(flag) # 提取所有手机号 phones re.findall(r1[3-9]\d{9}, html) print(phones) # 提取所有网址 urls re.findall(rhttp://.*?\.php, html) print(urls) .*? 非贪婪匹配CTF 提取 flag 必用防止匹配过多内容。 5简单面向对象 OOP看懂反序列化、SSTI 底层原理不用写大型类 只需掌握类、实例、属性、常用魔术方法 1基础用法 class User: # 构造方法实例化自动执行 def __init__(self, name, uid): self.name name self.uid uid def get_info(self): return f用户名:{self.name},ID:{self.uid} # 创建实例 u User(admin, 1) print(u.get_info()) print(u.name) 2关键魔术方法CTF SSTI / 反序列化核心 s abc print(s.__class__) # 获取所属类型 class str print(s.__class__.__bases__) # 获取父类 (object,) print(object.__subclasses__()) # 获取object所有子类SSTI执行命令核心 3反序列化漏洞原理 import pickle import os class Evil: # __reduce__ 在反序列化时自动触发 def __reduce__(self): return (os.system, (whoami,)) # 序列化生成恶意载荷 payload pickle.dumps(Evil()) # 反序列化触发命令执行 pickle.loads(payload) 4实战封装把 HTTP 扫描封装成类 把扫描功能封装代码复用运维脚本常用 class WebScan: def __init__(self, target): self.target target self.headers {User-Agent:Mozilla/5.0} def check_backend(self, path): url self.target path try: res requests.get(url, headersself.headers, timeout2) return res.status_code 200 except: return False scan WebScan(http://127.0.0.1) print(scan.check_backend(/admin))python爬虫前期尽量开始慢慢看懂代码再慢慢尝试自己写