JS

DOM介绍

DOM,英文是Document Object Model,中文名叫文档对象模型。
把一个html网页抽象为一个文档对象

01DOM元素查找

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. #div3{
  10. /* */
  11. background-color: aqua;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="div3">渗透测试</div>
  17. <div id="div2">web安全</div>
  18. <div id="div1">web基础</div>
  19. <ul id="ul1" class="cls1">
  20. <li>html</li>
  21. <li>css</li>
  22. <li>javascript</li>
  23. </ul>
  24. <ul id="ul2" class="cls2">
  25. <li>web漏洞</li>
  26. <li>代码审计</li>
  27. <li>提权维持</li>
  28. <li>服务器加固</li>
  29. </ul>
  30. <script type="text/javascript">
  31. // 通过id获取网页元素对象
  32. var div = document.getElementById("div1");
  33. console.log(div);
  34. // 通过标签名,获取一组网页元素
  35. var divs = document.getElementsByTagName("div");
  36. console.log(divs);
  37. // 获取一组元素,设置每一个元素的样式
  38. // 1. 获取父标签对象
  39. var ul = document.getElementById("ul1");
  40. console.log(ul);
  41. // 2. 获取子标签
  42. var lis = ul.getElementsByTagName("li");
  43. var len = lis.length;
  44. console.log(len);
  45. for(var i=0; i<len; i++){
  46. if(i == 1){
  47. // 样式属性,名称一般采用的是驼峰命名法
  48. lis[i].style.backgroundColor = "blue";
  49. }
  50. }
  51. </script>
  52. </body>
  53. </html>

02DOM的一些属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. #div3{
  10. /* */
  11. background-color: aqua;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="div3">渗透测试</div>
  17. <div id="div2">web安全</div>
  18. <div id="div1">web基础</div>
  19. <ul id="ul1" class="cls1">
  20. <li>html</li>
  21. <li>css</li>
  22. <li>javascript</li>
  23. </ul>
  24. <ul id="ul2" class="cls2">
  25. <li>web漏洞</li>
  26. <li>代码审计</li>
  27. <li>提权维持</li>
  28. <li>服务器加固</li>
  29. </ul>
  30. <script type="text/javascript">
  31. // 获取标签对象
  32. var div = document.getElementById("div1");
  33. // 获取innerHTML文本
  34. console.log(div.innerHTML);
  35. // 修改innerHTML文本
  36. div.innerHTML = "web漏洞";
  37. console.log(div.innerHTML);
  38. </script>
  39. </body>
  40. </html>

03DOMClass属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. #div3{
  10. /* */
  11. background-color: aqua;
  12. }
  13. .cls3{
  14. background-color: cyan;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="div3">渗透测试</div>
  20. <div id="div2">web安全</div>
  21. <div id="div1">web基础</div>
  22. <ul id="ul1" class="cls1">
  23. <li>html</li>
  24. <li>css</li>
  25. <li>javascript</li>
  26. </ul>
  27. <ul id="ul2" class="cls2">
  28. <li>web漏洞</li>
  29. <li>代码审计</li>
  30. <li>提权维持</li>
  31. <li>服务器加固</li>
  32. </ul>
  33. <script type="text/javascript">
  34. // 获取元素
  35. var ul = document.getElementById("ul1");
  36. // 获取类名
  37. console.log(ul.className);
  38. // 修改类属性
  39. ul.className = "cls3";
  40. </script>
  41. </body>
  42. </html>

04DOM自定义属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. /* #div3{
  10. background-color: aqua;
  11. } */
  12. .cls3{
  13. background-color: cyan;
  14. }
  15. .cls4{
  16. background-color: red;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="div3" class="cls3" hello="15pb">渗透测试</div>
  22. <div id="div2">web安全</div>
  23. <div id="div1">web基础</div>
  24. <ul id="ul1" class="cls1">
  25. <li>html</li>
  26. <li>css</li>
  27. <li>javascript</li>
  28. </ul>
  29. <ul id="ul2" class="cls2">
  30. <li>web漏洞</li>
  31. <li>代码审计</li>
  32. <li>提权维持</li>
  33. <li>服务器加固</li>
  34. </ul>
  35. <script type="text/javascript">
  36. var div = document.getElementById("div3");
  37. // 获取自定义属性
  38. var hello = div.getAttribute("hello");
  39. console.log(hello);
  40. // 获取本身有的一些属性
  41. var cls3 = div.getAttribute("class");
  42. console.log(cls3);
  43. // 设置属性
  44. div.setAttribute("hello", "world");
  45. div.setAttribute("class","cls4");
  46. // 重新获取输出
  47. var hello = div.getAttribute("hello");
  48. console.log(hello);
  49. var cls3 = div.getAttribute("class");
  50. console.log(cls3);
  51. // 删除属性
  52. div.removeAttribute("class");
  53. </script>
  54. </body>
  55. </html>

05DOM事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. /* #div3{
  10. background-color: aqua;
  11. } */
  12. .cls3{
  13. background-color: cyan;
  14. }
  15. .cls4{
  16. background-color: red;
  17. }
  18. #div1{
  19. width: 200px;
  20. height: 200px;
  21. background-color: aqua;
  22. }
  23. </style>
  24. </head>
  25. <body onload="fun1()">
  26. <input type="button" value="按钮" onclick="fun2()">
  27. <input type="text" name="" id="" onfocus="onfocus1()" onblur="onblur1()">
  28. <textarea name="" id="" cols="30" rows="10" onchange="onchange1()" ></textarea>
  29. <div id="div1" onmouseover="onmouseover1()" onmouseout="onmouseout1()"></div>
  30. <!--<div id="div3" class="cls3" hello="15pb">渗透测试</div>
  31. <div id="div2">web安全</div>
  32. <div id="div1">web基础</div>
  33. <ul id="ul1" class="cls1">
  34. <li>html</li>
  35. <li>css</li>
  36. <li>javascript</li>
  37. </ul>
  38. <ul id="ul2" class="cls2">
  39. <li>web漏洞</li>
  40. <li>代码审计</li>
  41. <li>提权维持</li>
  42. <li>服务器加固</li>
  43. </ul> -->
  44. <script type="text/javascript">
  45. // js中定义好函数
  46. function fun1(){
  47. console.log("hello world");
  48. }
  49. function fun2(){
  50. alert('测试');
  51. }
  52. function onmouseover1(){
  53. console.log("鼠标滑过");
  54. }
  55. function onmouseout1(){
  56. console.log("鼠标移出");
  57. }
  58. function onfocus1(){
  59. console.log("获取焦点");
  60. }
  61. function onblur1(){
  62. console.log("失去焦点");
  63. }
  64. function onchange1(){
  65. console.log("文本改变");
  66. }
  67. </script>
  68. </body>
  69. </html>

06DOM0事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. /* #div3{
  10. background-color: aqua;
  11. } */
  12. .cls3{
  13. background-color: cyan;
  14. }
  15. .cls4{
  16. background-color: red;
  17. }
  18. #div1{
  19. width: 200px;
  20. height: 200px;
  21. background-color: aqua;
  22. }
  23. </style>
  24. </head>
  25. <body onload="fun1()">
  26. <input type="button" value="按钮"" id="btn1">
  27. <input type="text" name="" id="edttext">
  28. <textarea name="" cols="30" rows="10" id="text1" ></textarea>
  29. <div id="div1" ></div>
  30. <!--<div id="div3" class="cls3" hello="15pb">渗透测试</div>
  31. <div id="div2">web安全</div>
  32. <div id="div1">web基础</div>
  33. <ul id="ul1" class="cls1">
  34. <li>html</li>
  35. <li>css</li>
  36. <li>javascript</li>
  37. </ul>
  38. <ul id="ul2" class="cls2">
  39. <li>web漏洞</li>
  40. <li>代码审计</li>
  41. <li>提权维持</li>
  42. <li>服务器加固</li>
  43. </ul> -->
  44. <script type="text/javascript">
  45. // js中定义好函数
  46. function fun1(){
  47. console.log("hello world");
  48. }
  49. function onclick1(){
  50. alert('测试');
  51. }
  52. function onmouseover1(){
  53. console.log("鼠标滑过");
  54. }
  55. function onmouseout1(){
  56. console.log("鼠标移出");
  57. }
  58. function onfocus1(){
  59. console.log("获取焦点");
  60. }
  61. function onblur1(){
  62. console.log("失去焦点");
  63. }
  64. function onchange1(){
  65. console.log("文本改变");
  66. }
  67. // 1. 获取元素
  68. var btn1 = document.getElementById("btn1");
  69. // 2. 指定事件
  70. btn1.onclick = onclick1;
  71. var edt = document.getElementById("edttext");
  72. // 函数变量赋值,onfocus1就是一个函数
  73. // 相当于 a=1, b=a, b=1
  74. edt.onfocus = onfocus1;
  75. // 匿名函数
  76. edt.onblur = function(){
  77. console.log("失去了焦点");
  78. }
  79. //
  80. var div = document.getElementById("div1");
  81. div.onmouseover = onmouseover1;
  82. div.onmouseout = onmouseout1;
  83. var text = document.getElementById("text1");
  84. text.onchange = onchange1;
  85. </script>
  86. </body>
  87. </html>

07DOM事件2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Document</title>
  8. <style type="text/css">
  9. /* #div3{
  10. background-color: aqua;
  11. } */
  12. .cls3 {
  13. background-color: cyan;
  14. }
  15. .cls4 {
  16. background-color: red;
  17. }
  18. #div1 {
  19. width: 200px;
  20. height: 200px;
  21. background-color: aqua;
  22. }
  23. </style>
  24. </head>
  25. <body onresize="onresize1()" onscroll="onscroll1()">
  26. <!-- 表单指定onsubmit属性,在提交按钮被点击时会触发 -->
  27. <form action="" onsubmit="sub()">
  28. <input type="button" value="按钮"" id="btn1">
  29. <input type="text" name="" id="edttext" />
  30. <input type="submit" />
  31. </form>
  32. <script type="text/javascript">
  33. function onresize1() {
  34. console.log("改变窗口");
  35. }
  36. function onscroll1() {
  37. console.log("滚动");
  38. }
  39. function sub() {
  40. alert("确认");
  41. }
  42. var btn1 = document.getElementById("btn1");
  43. btn1.onmousedown = function() {
  44. console.log("鼠标按下");
  45. };
  46. btn1.onmouseup = function() {
  47. console.log("鼠标弹起");
  48. };
  49. btn1.onmousemove = function() {
  50. console.log("鼠标移动");
  51. };
  52. </script>
  53. </body>
  54. </html>

08键盘事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <label id="label1">当前已输入</label><br/>
  11. <input type="button" value="按钮"" id="btn1">
  12. <input type="text" name="" id="edttext">
  13. <textarea name="" cols="30" rows="10" id="text1" ></textarea>
  14. <div id="div1" ></div>
  15. <script type="text/javascript">
  16. var text1 = document.getElementById("text1");
  17. text1.onkeydown = function(){
  18. // console.log("onkeydown");
  19. }
  20. // 数字、字母按下时才会触发
  21. var num1 = 0;
  22. // text1.onkeypress = function(){
  23. // console.log("onkeypress");
  24. // num1++;
  25. // // 1. 获取元素
  26. // var label1 = document.getElementById("label1");
  27. // // 2. 修改属性
  28. // label1.innerHTML = "当前已输入" + num1 + "个字符";
  29. // // 获取输入的字符
  30. // }
  31. text1.onkeypress = function(e){
  32. console.log("onkeypress");
  33. num1++;
  34. // 1. 获取元素
  35. var label1 = document.getElementById("label1");
  36. // 2. 修改属性
  37. label1.innerHTML = "当前已输入" + num1 + "个字符";
  38. // 获取输入的字符
  39. var ch = String.fromCharCode(e.keyCode);
  40. console.log(ch);
  41. }
  42. text1.onkeyup = function(){
  43. // console.log("onkeyup");
  44. }
  45. </script>
  46. </body>
  47. </html>

08DOM对象this

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style type="text/css">
  9. /* #btn1{
  10. width: 200px;
  11. } */
  12. </style>
  13. </head>
  14. <body>
  15. <label id="label1">当前已输入</label><br/>
  16. <input type="button" value="按钮"" id="btn1" onclick="onclick1(this)">
  17. <input type="button" value="按钮2"" id="btn2" onclick="onclick2(this)">
  18. <input type="text" name="" id="edttext">
  19. <textarea name="" cols="30" rows="10" id="text1" ></textarea>
  20. <div id="div1" ></div>
  21. <script type="text/javascript">
  22. function onclick1(){
  23. // this.value = "按钮2";
  24. var obj = arguments[0];
  25. obj.value = "按钮2";
  26. // 设置样式,对象.style.样式名
  27. obj.style.backgroundColor = "red";
  28. obj.style.width = "200px";
  29. console.log("onclick1");
  30. }
  31. function onclick2(obj){
  32. obj.value = "按钮2";
  33. // 设置样式,对象.style.样式名
  34. obj.style.height = "200px";
  35. obj.style.backgroundColor = "red";
  36. obj.style.width = "200px";
  37. console.log("onclick1");
  38. }
  39. </script>
  40. </body>
  41. </html>

09BOM对象window

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>BOM对象</title>
  8. </head>
  9. <body>
  10. <input type="button" value="按钮" onclick="onclick1()">
  11. <script type="text/javascript">
  12. function test(){
  13. // window对象
  14. var bret = window.confirm("你确定要做更改吗?");
  15. if(bret == true){
  16. window.alert("是的修改完毕");
  17. } else {
  18. window.alert("取消修改");
  19. }
  20. }
  21. function onclick1(){
  22. // window.open("./08键盘事件.html");
  23. var myWindow=window.open('','','width=200,height=100')
  24. myWindow.document.write("This is 'myWindow'")
  25. myWindow.focus();
  26. // 设置超时调用
  27. // 参数1,要执行的js代码
  28. // 参数2,超时毫秒数
  29. var timeid = setTimeout(function(){
  30. myWindow.close();
  31. // 清除时间调用
  32. clearTimeout(timeid);
  33. }, 1000);
  34. }
  35. </script>
  36. </body>
  37. </html>

10BOM对象间歇调用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <label id="label1">当前计时:0</label>
  11. <!-- <input type="text" name="" id="" /> -->
  12. <input type="button" value="计时开始" onclick="onclick1(this)" />
  13. <script type="text/javascript">
  14. var timecount = 0;
  15. var isStart = false;
  16. var timeid = null;
  17. function onclick1(obj) {
  18. if (isStart == false) {
  19. // 设置间歇调用
  20. timeid = setInterval(function() {
  21. // 1. 获取label
  22. var label1 = document.getElementById("label1");
  23. // 2. 修改label
  24. timecount++;
  25. label1.innerHTML = "当前计时:" + timecount;
  26. }, 1000);
  27. // 改变布尔值
  28. isStart = true;
  29. // 设置按钮文本
  30. obj.value = "关闭计时";
  31. } else {
  32. clearInterval(timeid);
  33. isStart = false;
  34. obj.value = "计时开始";
  35. // 1. 获取label
  36. var label1 = document.getElementById("label1");
  37. // 2. 修改label
  38. timecount = 0;
  39. label1.innerHTML = "当前计时:" + timecount;
  40. }
  41. }
  42. </script>
  43. </body>
  44. </html>

11BOM对象location对象

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <label id="label1">当前计时:0</label>
  11. <!-- <input type="text" name="" id="" /> -->
  12. <input type="button" value="按钮" onclick="onclick1(this)" />
  13. <script type="text/javascript">
  14. function onclick1(obj){
  15. // 获取当前文档的路径
  16. var url = location.href;
  17. // 路径是url编码形式的
  18. console.log(url);
  19. // 如果不包含hash信息,返回空
  20. var hash = location.hash;
  21. console.log(hash);
  22. // 如果不包含字段信息,返回空
  23. var host = location.host;
  24. console.log(host);
  25. var hostname = location.hostname;
  26. console.log(hostname);
  27. var pathname = location.pathname;
  28. console.log(pathname);
  29. var port = location.port;
  30. console.log(port);
  31. var protocol = location.protocol;
  32. console.log(protocol);
  33. var search = location.search;
  34. console.log(search);
  35. // 重新加载一下文档
  36. location.reload();
  37. // 打开一个新的文档,替换之
  38. location.replace("./02DOM的一些属性.html");
  39. }
  40. </script>
  41. </body>
  42. </html>

12BOM对象history

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <label id="label1">网页1</label><br />
  11. <!-- <input type="text" name="" id="" /> -->
  12. <input type="button" value="按钮1" onclick="onclick1(this)" />
  13. <input type="button" value="返回上一步" onclick="onclick2(this)" />
  14. <input type="button" value="前进一步" onclick="onclick3(this)" />
  15. <script type="text/javascript">
  16. function onclick1(obj) {
  17. window.open("./12BOM对象history.html", "", "", true);
  18. }
  19. function onclick2(obj) {
  20. // 返回上一个页面
  21. history.back();
  22. }
  23. function onclick3(obj) {
  24. // 前进一步
  25. history.forward();
  26. }
  27. </script>
  28. </body>
  29. </html>

13BOM对象history2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <label id="label1">网页2</label><br />
  11. <!-- <input type="text" name="" id="" /> -->
  12. <input type="button" value="获取历史记录" onclick="onclick0(this)" />
  13. <input type="button" value="指定跳转到某一步" onclick="onclick1(this)" />
  14. <input type="button" value="返回上一步" onclick="onclick2(this)" />
  15. <input type="button" value="前进一步" onclick="onclick3(this)" />
  16. <script type="text/javascript">
  17. function onclick0(obj) {
  18. var len = history.length;
  19. console.log("历史记录:"+ len + " " + history);
  20. }
  21. function onclick1(obj) {
  22. history.go(1);
  23. }
  24. function onclick2(obj) {
  25. // 返回上一个页面
  26. history.back();
  27. }
  28. function onclick3(obj) {
  29. // 返回上一个页面
  30. history.back();
  31. }
  32. function onclick3(obj) {
  33. // 前进一步
  34. history.forward();
  35. }
  36. </script>
  37. </body>
  38. </html>

14Screen对象属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <input type="button" value="获取屏幕信息" onclick="onclick0(this)" />
  11. <script type="text/javascript">
  12. function onclick0(obj){
  13. console.log("高度:" + screen.availHeight);
  14. console.log("宽度:" + screen.availWidth);
  15. }
  16. </script>
  17. </body>
  18. </html>

15BOM对象navigator对象

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script type="text/javascript">
  11. document.write("<p>UserAgent: ")
  12. document.write(navigator.userAgent + "</p>")
  13. </script>
  14. </body>
  15. </html>

python之urlib模块

python2.7

1.

  1. # coding=utf-8
  2. # py 2.7 ,urllib的使用,直接调用函数
  3. import requests
  4. import urllib
  5. url = "http://www.baidu.com";
  6. r = urllib.urlopen(url);
  7. print(r.getcode());
  8. print(r.geturl());
  9. print(r.info());
  10. # print(r.read());
  11. #print(r.readline());
  12. # print(r.readlines());
  13. #print(r.fileno());
  14. #print r.close();

2.

  1. # coding=utf-8
  2. # py 2.7 ,urllib的使用,直接调用函数
  3. import requests
  4. import urllib
  5. dict = {'name': '我是谁', 'age': 200}
  6. url = urllib.urlencode(dict);
  7. print url;

python3.7

  1. import urllib.request
  2. # py 3.7
  3. # urllib 是 python内置的HTTP请求库
  4. # urllib.request 请求模块
  5. # urllib.request.urlopen(url) 访问网页,返回源码
  6. # urllib.parse.urlencode(dict) 将dict或者包含两个元素的元组列表转换成url参数。
  7. # urllib.request.urlopen(url)
  8. # 创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据。
  9. #info():返回HTTPMessage对象,表示远程服务器返回的头信息。
  10. url = 'http://www.baidu.com'
  11. res = urllib.request.urlopen(url)
  12. # print(res.info())
  13. #getcode():返回Http状态码。
  14. # print(res.getcode())
  15. #geturl():返回请求的url。
  16. # print(res.geturl())
  17. # read(),readline(),readlines(),fileno(),close():对HTTPResponse类型数据进行操作。
  18. # print(res.read())
  19. # urllib.parse.urlencode(dict)
  20. #将dict或者包含两个元素的元组列表转换成url参数。
  21. #如:字典{'name': ‘hell15pb', 'age': 200}将被转换为"name=hell15pb&age=200"
  22. # https://tieba.baidu.com/f?ie=utf-8&kw=篮球&fr=search
  23. dict = {'ie': 'utf-8','kw': '篮球','fr':'search'}
  24. url = 'https://tieba.baidu.com/f?'
  25. data = urllib.parse.urlencode(dict)
  26. print(url+data)

python之re模块实例

1.

  1. import urllib.request
  2. import re
  3. # python 3.7
  4. url = 'https://www.huya.com/'
  5. r = urllib.request.urlopen(url);
  6. # print(r.read().decode('utf-8'))
  7. ret = re.findall('target="_blank">(.*)</a></dd>', r.read().decode('utf-8'));
  8. for i in range(0,len(ret)):
  9. print(ret[i])

2.

  1. import re
  2. import urllib.request
  3. url = 'http://jandan.net/ooxx'
  4. res = urllib.request.urlopen(url)
  5. # print(res.read().decode('utf-8'))
  6. ret = re.findall('</span><p><a href="(.*)" target="_blank"', res.read().decode('utf-8'));
  7. for i in range(0,len(ret)):
  8. print("http:"+ret[i])