A. 怎麼爬取網頁的動態內容,很多都是js動態生
抓取動態頁面有兩種常用的方法,一是通過javaScript逆向工程獲取動態數據介面(真實的訪問路徑),另一種是利用selenium庫模擬真實瀏覽器,獲取JavaScript渲染後的內容。但selenium庫用起來比較繁瑣,抓取速度相對較慢,所以第一種方法日常使用較多。
B. 只用js如何實現表格內容的動態修改
<body>
<tableid='test'>//定義一個table
<tr>
<td></td><td></td>
</tr>
</table>
<script>
vartb=document.getElementById('test');//獲取表格的dom節點
vartd=tb.rows[0].cells[0];//獲取0行0列的td單元格
td.innerHTML='222';//動態修改表格的內容為222
</script>
</body>
思路:
1、獲取表格的dom節點
2、通過rows和cells定位td單元格
3、通過修改innerHTML
JS實現動態表格的新增,修改,刪除操作
一、相關JS函數
function setParamslist() {
var tab = document.getElementById("tab");
//表格行數
var rows = tab.rows.length ;
//表格列數
var cells = tab.rows.item(0).cells.length ;
//alert("行數"+rows+"列數"+cells);
var rowData = "";
for(var i=1;i<rows;i++) {
var cellsData = new Array();
for(var j=0;j<cells-1;j++) {
cellsData.push(tab.rows[i].cells[j].innerText);
}
rowData = rowData + "|" + cellsData;
}
document.getElementById("paramslist").value = rowData;
}
//打開相關新增應用參數界面
function openAppParamsPage() {
var param = new Object();
//這個參數一定要傳。
param.win = window;
param.id = 100;
param.name = "test";
param.birthday = new Date();
var result = window.showModalDialog("addParamsItem","dialogWidth:500px;dialogHeight:600px;dialogLeft:200px;dialogTop=200px");
//var temp = document.getElementById("paramslist").value;
//document.getElementById("paramslist").value = temp + result;
addSort(result);
}
// 增加應用參數函數
function addSort(data) {
var name = data;
if(name == ""||name==undefined ) {
return;
}
console.log(data);
var params = data.split(",");
var paramName = params[0];
var paramCode = params[1];
var paramValue = params[2];
var row = document.createElement("tr");
row.setAttribute("id", paramCode);
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramName));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramCode));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramValue));
row.appendChild(cell);
var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "刪除");
deleteButton.onclick = function () { deleteSort(paramCode); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);
document.getElementById("sortList").appendChild(row);
}
// 刪除應用參數函數
function deleteSort(id) {
if (id!=null){
var rowToDelete = document.getElementById(id);
var sortList = document.getElementById("sortList");
sortList.removeChild(rowToDelete);
}
}
二、彈出框頁面,新增或者修改參數,並回寫相關數據。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增應用</title>
<#include "/views/head.html"/>
</head>
<body>
<div class="body-box">
<div class="clear"></div>
<form >
<table width="100%" cellspacing="1" cellpadding="2" border="0"class="pn-ftable">
<tr>
<td>參數名稱:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramName" name="paramName"/></td>
</tr>
<tr>
<td>參數編碼:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramCode" name="paramCode" required="true" /></td>
</tr>
<tr>
<td>參數值:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramValue" name="paramValue" required="true" /></td>
</tr>
<tr>
<td align="center" colspan="4">
<input type="submit" value="保存" onclick="returnResult();"/>
<input type="button" value="返回" onclick="closeWindow();"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<script type="text/javascript">
//直接關閉窗口
function closeWindow() {
window.close();
}
//獲取值,組裝後返回
function returnResult() {
if(!$('form').valid())
return;
var paramName = document.getElementById("paramName");
var paramCode = document.getElementById("paramCode");
var paramValue = document.getElementById("paramValue");
//alert("value is " + paramName.value + "," + paramCode.value + "," + paramValue.value);
var result = paramName.value + "," + paramCode.value + "," + paramValue.value;
window.returnValue = result;
window.close();
}
</script>
C. 如何用python爬取js動態生成內容的頁面
抓取js動態生成的內容的頁面有兩種基本的解決方案
1用dryscrape庫動態抓取頁面
js腳本是通過瀏覽器來執行並返回信息的,所以,抓取js執行後的頁面,一個最直接的方式就是用python模擬瀏覽器的行為。WebKit 是一個開源的瀏覽器引擎,python提供了許多庫可以調用這個引擎,dryscrape便是其中之一,它調用webkit引擎來處理包含js等的網頁!
2 selenium web測試框架
selenium是一個web測試框架,它允許調用本地的瀏覽器引擎發送網頁請求,所以,它同樣可以實現抓取頁面的要求。
D. js動態添加內容
第一個:a,創建一個img元素,然後添加到div節點下面。
第二個:innerHTML , 直接用HTML代碼加入。
<!--
varimg=document_createElement_x('img'),
fff=document.getElementByIdx_x('aaa');
img.setAttribute('src','http://www.blueidea.com/articleimg/bbsimg/topic5.gif');
img.style.position='absolute';
img.style.left=fff.offsetLeft+20+'px';
img.style.top=fff.offsetTop+30+'px';
fff.a(img);
//-->
<script>
onload=functionf(){
document.getElementByIdx_x_x("insert").innerHTML='<imgsrc="new_banner.jpg"height="100"width="100"/>';
}
</script>
<divid="insert"></div>
E. js動態顯示文本內容
<?php
$filename="1.txt";
$filename2="2.txt";
$handle=fopen($filename,"r");
$handle2=fopen($filename2,"r");
//通過filesize獲得文件大小,將整個文件一下子讀到一個字元串中
$contents=fread($handle,filesize($filename));
$contents2=fread($handle2,filesize($filename2));
fclose($handle);
var_mp($contents);
var_mp($contents2);
echo'<br>';
echo"$contents-$contents2*2";
echo'<br>';
echo$contents-$contents2*2;
?>
你可以將這兩個數值傳遞到前端用js計算. 反正已經知道了結果.
至於實時動態顯示最新的結果. 如果頁面不刷新也可以獲取, 那麼就用ajax做或者websocket做.
F. 只用js如何實現表格內容的動態修改
<body>
<tableid='test'>//定義一個table
<tr>
<td></td><td></td>
</tr>
</table>
<script>
vartb=document.getElementById('test');//獲取表格的dom節點
vartd=tb.rows[0].cells[0];//獲取0行0列的td單元格
td.innerHTML='222';//動態修改表格的內容為222
</script>
</body>
思路:
1、獲取表格的dom節點
2、通過rows和cells定位td單元格
3、通過修改innerHTML
JS實現動態表格的新增,修改,刪除操作
一、相關JS函數
function setParamslist() {
var tab = document.getElementById("tab");
//表格行數
var rows = tab.rows.length ;
//表格列數
var cells = tab.rows.item(0).cells.length ;
//alert("行數"+rows+"列數"+cells);
var rowData = "";
for(var i=1;i<rows;i++) {
var cellsData = new Array();
for(var j=0;j<cells-1;j++) {
cellsData.push(tab.rows[i].cells[j].innerText);
}
rowData = rowData + "|" + cellsData;
}
document.getElementById("paramslist").value = rowData;
}
//打開相關新增應用參數界面
function openAppParamsPage() {
var param = new Object();
//這個參數一定要傳。
param.win = window;
param.id = 100;
param.name = "test";
param.birthday = new Date();
var result = window.showModalDialog("addParamsItem","dialogWidth:500px;dialogHeight:600px;dialogLeft:200px;dialogTop=200px");
//var temp = document.getElementById("paramslist").value;
//document.getElementById("paramslist").value = temp + result;
addSort(result);
}
// 增加應用參數函數
function addSort(data) {
var name = data;
if(name == ""||name==undefined ) {
return;
}
console.log(data);
var params = data.split(",");
var paramName = params[0];
var paramCode = params[1];
var paramValue = params[2];
var row = document.createElement("tr");
row.setAttribute("id", paramCode);
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramName));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramCode));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramValue));
row.appendChild(cell);
var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "刪除");
deleteButton.onclick = function () { deleteSort(paramCode); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);
document.getElementById("sortList").appendChild(row);
}
// 刪除應用參數函數
function deleteSort(id) {
if (id!=null){
var rowToDelete = document.getElementById(id);
var sortList = document.getElementById("sortList");
sortList.removeChild(rowToDelete);
}
}
二、彈出框頁面,新增或者修改參數,並回寫相關數據。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增應用</title>
<#include "/views/head.html"/>
</head>
<body>
<div class="body-box">
<div class="clear"></div>
<form >
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="pn-ftable">
<tr>
<td>參數名稱:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramName" name="paramName"/></td>
</tr>
<tr>
<td>參數編碼:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramCode" name="paramCode" required="true" /></td>
</tr>
<tr>
<td>參數值:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramValue" name="paramValue" required="true" /></td>
</tr>
<tr>
<td align="center" colspan="4">
<input type="submit" value="保存" onclick="returnResult();"/>
<input type="button" value="返回" onclick="closeWindow();"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<script type="text/javascript">
//直接關閉窗口
function closeWindow() {
window.close();
}
//獲取值,組裝後返回
function returnResult() {
if(!$('form').valid())
return;
var paramName = document.getElementById("paramName");
var paramCode = document.getElementById("paramCode");
var paramValue = document.getElementById("paramValue");
//alert("value is " + paramName.value + "," + paramCode.value + "," + paramValue.value);
var result = paramName.value + "," + paramCode.value + "," + paramValue.value;
window.returnValue = result;
window.close();
}
</script>
G. js動態獲取文本框的值
可以使用原生js的document.getElementById("id")方法
document.getElementById("id").value
也可以使用jQuery來獲取
$("#id").val()
H. js動態添加html內容
<!DOCTYPEHTML>
<html>
<head>
<title>yugi</title>
<metacharset=UTF-8/>
<styletype="text/css">
</style>
<scripttype="text/javascript">
varadd=function(dom)
{
varp=dom.parentElement,body=document.body,len=body.children.length;
varhtml=p.innerHTML.replace(p.children[p.children.length-1].outerHTML,"")
.replace(/(name["'=]+radio)[^"'s>]+/gim,'$1'+(len+1));
body.innerHTML+="<div>"+html+"</div>";
}
</script>
</head>
<body>
<body>
<div>
<label><inputtype="radio"name="radio1"value="學生"checked="checked"/>A.學生</label>
<label><inputtype="radio"name="radio1"value="教師"/>B.教師</label>
<label><inputtype="radio"name="radio1"value="管理員"/>C.管理員</label>
<label><inputtype="radio"name="radio1"value="管理員"/>D.管理員</label>
<inputtype="button"value="添加"name="add"onclick="add(this);">
</div>
</body>
</html>
I. 只用js如何實現表格內容的動態修改
<body>
<tableid='test'>//定義一個table
<tr>
<td></td><td></td>
</tr>
</table>
<script>
vartb=document.getElementById('test');//獲取表格的dom節點
vartd=tb.rows[0].cells[0];//獲取0行0列的td單元格
td.innerHTML='222';//動態修改表格的內容為222
</script>
</body>
思路:
1、獲取表格的dom節點
2、通過rows和cells定位td單元格
3、通過修改innerHTML
JS實現動態表格的新增,修改,刪除操作
一、相關JS函數
function setParamslist() {
var tab = document.getElementById("tab");
//表格行數
var rows = tab.rows.length ;
//表格列數
var cells = tab.rows.item(0).cells.length ;
//alert("行數"+rows+"列數"+cells);
var rowData = "";
for(var i=1;i<rows;i++) {
var cellsData = new Array();
for(var j=0;j<cells-1;j++) {
cellsData.push(tab.rows[i].cells[j].innerText);
}
rowData = rowData + "|" + cellsData;
}
document.getElementById("paramslist").value = rowData;
}
//打開相關新增應用參數界面
function openAppParamsPage() {
var param = new Object();
//這個參數一定要傳。
param.win = window;
param.id = 100;
param.name = "test";
param.birthday = new Date();
var result = window.showModalDialog("addParamsItem","dialogWidth:500px;dialogHeight:600px;dialogLeft:200px;dialogTop=200px");
//var temp = document.getElementById("paramslist").value;
//document.getElementById("paramslist").value = temp + result;
addSort(result);
}
// 增加應用參數函數
function addSort(data) {
var name = data;
if(name == ""||name==undefined ) {
return;
}
console.log(data);
var params = data.split(",");
var paramName = params[0];
var paramCode = params[1];
var paramValue = params[2];
var row = document.createElement("tr");
row.setAttribute("id", paramCode);
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramName));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramCode));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramValue));
row.appendChild(cell);
var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "刪除");
deleteButton.onclick = function () { deleteSort(paramCode); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);
document.getElementById("sortList").appendChild(row);
}
// 刪除應用參數函數
function deleteSort(id) {
if (id!=null){
var rowToDelete = document.getElementById(id);
var sortList = document.getElementById("sortList");
sortList.removeChild(rowToDelete);
}
}
二、彈出框頁面,新增或者修改參數,並回寫相關數據。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增應用</title>
<#include "/views/head.html"/>
</head>
<body>
<div class="body-box">
<div class="clear"></div>
<form >
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="pn-ftable">
<tr>
<td>參數名稱:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramName" name="paramName"/></td>
</tr>
<tr>
<td>參數編碼:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramCode" name="paramCode" required="true" /></td>
</tr>
<tr>
<td>參數值:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramValue" name="paramValue" required="true" /></td>
</tr>
<tr>
<td align="center" colspan="4">
<input type="submit" value="保存" onclick="returnResult();"/>
<input type="button" value="返回" onclick="closeWindow();"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<script type="text/javascript">
//直接關閉窗口
function closeWindow() {
window.close();
}
//獲取值,組裝後返回
function returnResult() {
if(!$('form').valid())
return;
var paramName = document.getElementById("paramName");
var paramCode = document.getElementById("paramCode");
var paramValue = document.getElementById("paramValue");
//alert("value is " + paramName.value + "," + paramCode.value + "," + paramValue.value);
var result = paramName.value + "," + paramCode.value + "," + paramValue.value;
window.returnValue = result;
window.close();
}
</script>