基于python编写简单的web目录扫描(随机ua,随机代理ip)脚本
随便看看吧,
记录编写脚本过程。
核心代码如下,后期再优化多线程:
import requests
import random
#自定义url
url = 'http://www.baidu.com'
#随机ua
user_agent_list = [
# Firefox
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
# Safari
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
# QQ浏览器
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0)",
]
url_agent = random.choice(user_agent_list)
#代理池,可添加
porxy_list = {
"http":"http://127.0.0.1:7890"
}
#请求头
headers = {
"User-Agent":url_agent
}
url_list =[]
if dir =="":
#自定义字典
dir = "test.txt"
try:
with open("test.txt",'r')as f:
for a in f:
a = a.replace('\n','')
url_list.append(a)
f.close()
except Exception as e:
print(e)
for data in url_list:
dst_url = url + data
try:
response = requests.get(dst_url,headers=headers,proxies=porxy_list)
print("%s-----%s" %(dst_url,response))
except Exception as e:
print("%s-----%s" %(dst_url,e))
结果如下: