JS
DOM介绍
DOM,英文是Document Object Model,中文名叫文档对象模型。
把一个html网页抽象为一个文档对象
01DOM元素查找
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
#div3{
/* */
background-color: aqua;
}
</style>
</head>
<body>
<div id="div3">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul>
<script type="text/javascript">
// 通过id获取网页元素对象
var div = document.getElementById("div1");
console.log(div);
// 通过标签名,获取一组网页元素
var divs = document.getElementsByTagName("div");
console.log(divs);
// 获取一组元素,设置每一个元素的样式
// 1. 获取父标签对象
var ul = document.getElementById("ul1");
console.log(ul);
// 2. 获取子标签
var lis = ul.getElementsByTagName("li");
var len = lis.length;
console.log(len);
for(var i=0; i<len; i++){
if(i == 1){
// 样式属性,名称一般采用的是驼峰命名法
lis[i].style.backgroundColor = "blue";
}
}
</script>
</body>
</html>
02DOM的一些属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
#div3{
/* */
background-color: aqua;
}
</style>
</head>
<body>
<div id="div3">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul>
<script type="text/javascript">
// 获取标签对象
var div = document.getElementById("div1");
// 获取innerHTML文本
console.log(div.innerHTML);
// 修改innerHTML文本
div.innerHTML = "web漏洞";
console.log(div.innerHTML);
</script>
</body>
</html>
03DOMClass属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
#div3{
/* */
background-color: aqua;
}
.cls3{
background-color: cyan;
}
</style>
</head>
<body>
<div id="div3">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul>
<script type="text/javascript">
// 获取元素
var ul = document.getElementById("ul1");
// 获取类名
console.log(ul.className);
// 修改类属性
ul.className = "cls3";
</script>
</body>
</html>
04DOM自定义属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
/* #div3{
background-color: aqua;
} */
.cls3{
background-color: cyan;
}
.cls4{
background-color: red;
}
</style>
</head>
<body>
<div id="div3" class="cls3" hello="15pb">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul>
<script type="text/javascript">
var div = document.getElementById("div3");
// 获取自定义属性
var hello = div.getAttribute("hello");
console.log(hello);
// 获取本身有的一些属性
var cls3 = div.getAttribute("class");
console.log(cls3);
// 设置属性
div.setAttribute("hello", "world");
div.setAttribute("class","cls4");
// 重新获取输出
var hello = div.getAttribute("hello");
console.log(hello);
var cls3 = div.getAttribute("class");
console.log(cls3);
// 删除属性
div.removeAttribute("class");
</script>
</body>
</html>
05DOM事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
/* #div3{
background-color: aqua;
} */
.cls3{
background-color: cyan;
}
.cls4{
background-color: red;
}
#div1{
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body onload="fun1()">
<input type="button" value="按钮" onclick="fun2()">
<input type="text" name="" id="" onfocus="onfocus1()" onblur="onblur1()">
<textarea name="" id="" cols="30" rows="10" onchange="onchange1()" ></textarea>
<div id="div1" onmouseover="onmouseover1()" onmouseout="onmouseout1()"></div>
<!--<div id="div3" class="cls3" hello="15pb">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul> -->
<script type="text/javascript">
// js中定义好函数
function fun1(){
console.log("hello world");
}
function fun2(){
alert('测试');
}
function onmouseover1(){
console.log("鼠标滑过");
}
function onmouseout1(){
console.log("鼠标移出");
}
function onfocus1(){
console.log("获取焦点");
}
function onblur1(){
console.log("失去焦点");
}
function onchange1(){
console.log("文本改变");
}
</script>
</body>
</html>
06DOM0事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
/* #div3{
background-color: aqua;
} */
.cls3{
background-color: cyan;
}
.cls4{
background-color: red;
}
#div1{
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body onload="fun1()">
<input type="button" value="按钮"" id="btn1">
<input type="text" name="" id="edttext">
<textarea name="" cols="30" rows="10" id="text1" ></textarea>
<div id="div1" ></div>
<!--<div id="div3" class="cls3" hello="15pb">渗透测试</div>
<div id="div2">web安全</div>
<div id="div1">web基础</div>
<ul id="ul1" class="cls1">
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<ul id="ul2" class="cls2">
<li>web漏洞</li>
<li>代码审计</li>
<li>提权维持</li>
<li>服务器加固</li>
</ul> -->
<script type="text/javascript">
// js中定义好函数
function fun1(){
console.log("hello world");
}
function onclick1(){
alert('测试');
}
function onmouseover1(){
console.log("鼠标滑过");
}
function onmouseout1(){
console.log("鼠标移出");
}
function onfocus1(){
console.log("获取焦点");
}
function onblur1(){
console.log("失去焦点");
}
function onchange1(){
console.log("文本改变");
}
// 1. 获取元素
var btn1 = document.getElementById("btn1");
// 2. 指定事件
btn1.onclick = onclick1;
var edt = document.getElementById("edttext");
// 函数变量赋值,onfocus1就是一个函数
// 相当于 a=1, b=a, b=1
edt.onfocus = onfocus1;
// 匿名函数
edt.onblur = function(){
console.log("失去了焦点");
}
//
var div = document.getElementById("div1");
div.onmouseover = onmouseover1;
div.onmouseout = onmouseout1;
var text = document.getElementById("text1");
text.onchange = onchange1;
</script>
</body>
</html>
07DOM事件2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style type="text/css">
/* #div3{
background-color: aqua;
} */
.cls3 {
background-color: cyan;
}
.cls4 {
background-color: red;
}
#div1 {
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body onresize="onresize1()" onscroll="onscroll1()">
<!-- 表单指定onsubmit属性,在提交按钮被点击时会触发 -->
<form action="" onsubmit="sub()">
<input type="button" value="按钮"" id="btn1">
<input type="text" name="" id="edttext" />
<input type="submit" />
</form>
<script type="text/javascript">
function onresize1() {
console.log("改变窗口");
}
function onscroll1() {
console.log("滚动");
}
function sub() {
alert("确认");
}
var btn1 = document.getElementById("btn1");
btn1.onmousedown = function() {
console.log("鼠标按下");
};
btn1.onmouseup = function() {
console.log("鼠标弹起");
};
btn1.onmousemove = function() {
console.log("鼠标移动");
};
</script>
</body>
</html>
08键盘事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<label id="label1">当前已输入</label><br/>
<input type="button" value="按钮"" id="btn1">
<input type="text" name="" id="edttext">
<textarea name="" cols="30" rows="10" id="text1" ></textarea>
<div id="div1" ></div>
<script type="text/javascript">
var text1 = document.getElementById("text1");
text1.onkeydown = function(){
// console.log("onkeydown");
}
// 数字、字母按下时才会触发
var num1 = 0;
// text1.onkeypress = function(){
// console.log("onkeypress");
// num1++;
// // 1. 获取元素
// var label1 = document.getElementById("label1");
// // 2. 修改属性
// label1.innerHTML = "当前已输入" + num1 + "个字符";
// // 获取输入的字符
// }
text1.onkeypress = function(e){
console.log("onkeypress");
num1++;
// 1. 获取元素
var label1 = document.getElementById("label1");
// 2. 修改属性
label1.innerHTML = "当前已输入" + num1 + "个字符";
// 获取输入的字符
var ch = String.fromCharCode(e.keyCode);
console.log(ch);
}
text1.onkeyup = function(){
// console.log("onkeyup");
}
</script>
</body>
</html>
08DOM对象this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
/* #btn1{
width: 200px;
} */
</style>
</head>
<body>
<label id="label1">当前已输入</label><br/>
<input type="button" value="按钮"" id="btn1" onclick="onclick1(this)">
<input type="button" value="按钮2"" id="btn2" onclick="onclick2(this)">
<input type="text" name="" id="edttext">
<textarea name="" cols="30" rows="10" id="text1" ></textarea>
<div id="div1" ></div>
<script type="text/javascript">
function onclick1(){
// this.value = "按钮2";
var obj = arguments[0];
obj.value = "按钮2";
// 设置样式,对象.style.样式名
obj.style.backgroundColor = "red";
obj.style.width = "200px";
console.log("onclick1");
}
function onclick2(obj){
obj.value = "按钮2";
// 设置样式,对象.style.样式名
obj.style.height = "200px";
obj.style.backgroundColor = "red";
obj.style.width = "200px";
console.log("onclick1");
}
</script>
</body>
</html>
09BOM对象window
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BOM对象</title>
</head>
<body>
<input type="button" value="按钮" onclick="onclick1()">
<script type="text/javascript">
function test(){
// window对象
var bret = window.confirm("你确定要做更改吗?");
if(bret == true){
window.alert("是的修改完毕");
} else {
window.alert("取消修改");
}
}
function onclick1(){
// window.open("./08键盘事件.html");
var myWindow=window.open('','','width=200,height=100')
myWindow.document.write("This is 'myWindow'")
myWindow.focus();
// 设置超时调用
// 参数1,要执行的js代码
// 参数2,超时毫秒数
var timeid = setTimeout(function(){
myWindow.close();
// 清除时间调用
clearTimeout(timeid);
}, 1000);
}
</script>
</body>
</html>
10BOM对象间歇调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<label id="label1">当前计时:0</label>
<!-- <input type="text" name="" id="" /> -->
<input type="button" value="计时开始" onclick="onclick1(this)" />
<script type="text/javascript">
var timecount = 0;
var isStart = false;
var timeid = null;
function onclick1(obj) {
if (isStart == false) {
// 设置间歇调用
timeid = setInterval(function() {
// 1. 获取label
var label1 = document.getElementById("label1");
// 2. 修改label
timecount++;
label1.innerHTML = "当前计时:" + timecount;
}, 1000);
// 改变布尔值
isStart = true;
// 设置按钮文本
obj.value = "关闭计时";
} else {
clearInterval(timeid);
isStart = false;
obj.value = "计时开始";
// 1. 获取label
var label1 = document.getElementById("label1");
// 2. 修改label
timecount = 0;
label1.innerHTML = "当前计时:" + timecount;
}
}
</script>
</body>
</html>
11BOM对象location对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<label id="label1">当前计时:0</label>
<!-- <input type="text" name="" id="" /> -->
<input type="button" value="按钮" onclick="onclick1(this)" />
<script type="text/javascript">
function onclick1(obj){
// 获取当前文档的路径
var url = location.href;
// 路径是url编码形式的
console.log(url);
// 如果不包含hash信息,返回空
var hash = location.hash;
console.log(hash);
// 如果不包含字段信息,返回空
var host = location.host;
console.log(host);
var hostname = location.hostname;
console.log(hostname);
var pathname = location.pathname;
console.log(pathname);
var port = location.port;
console.log(port);
var protocol = location.protocol;
console.log(protocol);
var search = location.search;
console.log(search);
// 重新加载一下文档
location.reload();
// 打开一个新的文档,替换之
location.replace("./02DOM的一些属性.html");
}
</script>
</body>
</html>
12BOM对象history
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<label id="label1">网页1</label><br />
<!-- <input type="text" name="" id="" /> -->
<input type="button" value="按钮1" onclick="onclick1(this)" />
<input type="button" value="返回上一步" onclick="onclick2(this)" />
<input type="button" value="前进一步" onclick="onclick3(this)" />
<script type="text/javascript">
function onclick1(obj) {
window.open("./12BOM对象history.html", "", "", true);
}
function onclick2(obj) {
// 返回上一个页面
history.back();
}
function onclick3(obj) {
// 前进一步
history.forward();
}
</script>
</body>
</html>
13BOM对象history2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<label id="label1">网页2</label><br />
<!-- <input type="text" name="" id="" /> -->
<input type="button" value="获取历史记录" onclick="onclick0(this)" />
<input type="button" value="指定跳转到某一步" onclick="onclick1(this)" />
<input type="button" value="返回上一步" onclick="onclick2(this)" />
<input type="button" value="前进一步" onclick="onclick3(this)" />
<script type="text/javascript">
function onclick0(obj) {
var len = history.length;
console.log("历史记录:"+ len + " " + history);
}
function onclick1(obj) {
history.go(1);
}
function onclick2(obj) {
// 返回上一个页面
history.back();
}
function onclick3(obj) {
// 返回上一个页面
history.back();
}
function onclick3(obj) {
// 前进一步
history.forward();
}
</script>
</body>
</html>
14Screen对象属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="button" value="获取屏幕信息" onclick="onclick0(this)" />
<script type="text/javascript">
function onclick0(obj){
console.log("高度:" + screen.availHeight);
console.log("宽度:" + screen.availWidth);
}
</script>
</body>
</html>
15BOM对象navigator对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
document.write("<p>UserAgent: ")
document.write(navigator.userAgent + "</p>")
</script>
</body>
</html>
python之urlib模块
python2.7
1.
# coding=utf-8
# py 2.7 ,urllib的使用,直接调用函数
import requests
import urllib
url = "http://www.baidu.com";
r = urllib.urlopen(url);
print(r.getcode());
print(r.geturl());
print(r.info());
# print(r.read());
#print(r.readline());
# print(r.readlines());
#print(r.fileno());
#print r.close();
2.
# coding=utf-8
# py 2.7 ,urllib的使用,直接调用函数
import requests
import urllib
dict = {'name': '我是谁', 'age': 200}
url = urllib.urlencode(dict);
print url;
python3.7
import urllib.request
# py 3.7
# urllib 是 python内置的HTTP请求库
# urllib.request 请求模块
# urllib.request.urlopen(url) 访问网页,返回源码
# urllib.parse.urlencode(dict) 将dict或者包含两个元素的元组列表转换成url参数。
# urllib.request.urlopen(url)
# 创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据。
#info():返回HTTPMessage对象,表示远程服务器返回的头信息。
url = 'http://www.baidu.com'
res = urllib.request.urlopen(url)
# print(res.info())
#getcode():返回Http状态码。
# print(res.getcode())
#geturl():返回请求的url。
# print(res.geturl())
# read(),readline(),readlines(),fileno(),close():对HTTPResponse类型数据进行操作。
# print(res.read())
# urllib.parse.urlencode(dict)
#将dict或者包含两个元素的元组列表转换成url参数。
#如:字典{'name': ‘hell15pb', 'age': 200}将被转换为"name=hell15pb&age=200"
# https://tieba.baidu.com/f?ie=utf-8&kw=篮球&fr=search
dict = {'ie': 'utf-8','kw': '篮球','fr':'search'}
url = 'https://tieba.baidu.com/f?'
data = urllib.parse.urlencode(dict)
print(url+data)
python之re模块实例
1.
import urllib.request
import re
# python 3.7
url = 'https://www.huya.com/'
r = urllib.request.urlopen(url);
# print(r.read().decode('utf-8'))
ret = re.findall('target="_blank">(.*)</a></dd>', r.read().decode('utf-8'));
for i in range(0,len(ret)):
print(ret[i])
2.
import re
import urllib.request
url = 'http://jandan.net/ooxx'
res = urllib.request.urlopen(url)
# print(res.read().decode('utf-8'))
ret = re.findall('</span><p><a href="(.*)" target="_blank"', res.read().decode('utf-8'));
for i in range(0,len(ret)):
print("http:"+ret[i])