信
51rgb
網(wǎng)頁自動跳轉(zhuǎn)頁面的代碼在很多時候都非常的有用,下面的是兩個簡單的例子。僅供參考。
方案一,用<meta>里直接寫刷新語句:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-CN">
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<meta http-equiv="refresh" content="5;url=http://www.51rgb.com">
<title>html網(wǎng)頁自動跳轉(zhuǎn)代碼--西農(nóng)大網(wǎng)站</title>
</head>
<body>
測試:html網(wǎng)頁自動跳轉(zhuǎn)代碼<br/>
你可以在這里寫下你想的一切東西!<br />
如:西北農(nóng)林科技大學(xué)是一所985、211院校。<br />
</body></html>
方案二,用javascript腳本來跳轉(zhuǎn)
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>正在進入西農(nóng)大網(wǎng)站</title>
</head>
<body>
<form name=loading>
<p align=center> <font color="#0066ff" size="2">正在進入,請稍等</font><font color="#0066ff" size="2" face="Arial">...</font>
<input type=text name=chart size=46 style="font-family:Arial; font-weight:bolder; color:#0066ff; background-color:#fef4d9; padding:0px; border-style:none;">
<inputtype=text name=percent size=47 style="color:#0066ff; text-align:center; border-width:medium; border-style:none;">
<script>
var bar=0
var line="||"
var amount="||"
count
function count{
bar=bar+2
amount =amount + line
document.loading.chart.value=amount
document.loading.percent.value=bar+"%"
if (bar<99)
{setTimeout("count",100);}
else
{window.location =http://www.51rgb.com;}
}</script>
</p>
</form><p align="center">
測試:html網(wǎng)頁自動跳轉(zhuǎn)代碼<br/>
你可以在這里寫下你想的一切東西!<br />
如:西北農(nóng)林科技大學(xué)是一所985、211院校。<br /><br />
如果您的瀏覽器不支持跳轉(zhuǎn),<astyle="text-decoration: none" ><font color="#FF0000">請點這里</font></a>.</p>
</body>
</html>
↓↓↓
目中經(jīng)常會出現(xiàn)點擊跳轉(zhuǎn)錨點的方法,比如給一個a標簽一個href=“#錨點”,然后要跳的錨點給個id=“錨點”,這樣就實現(xiàn)簡單的跳轉(zhuǎn),但是這樣在url地址欄后面都會出現(xiàn)一個諸如www.csdn.net#錨點,然后你點擊給一次后退都是退回上一個選擇的錨點url,這里總結(jié)一些跳轉(zhuǎn)錨點的方法。
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 800px;
width: 400px;
border: 2px solid black;
}
h2 {
position: fixed;
margin:50px 500px;
}
</style>
</head>
<body>
<h2>
<a href="#div1">to div1</a>
<a href="#div2">to div2</a>
<a href="#div3">to div3</a>
</h2>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
</body>
</html>
這種方法的缺點是點擊錨點之后,瀏覽器的URL會發(fā)生變化,如果刷新可能會出現(xiàn)問題。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#div1Link").click(function() {
$("html, body").animate({
scrollTop: $("#div1").offset().top }, {duration: 500,easing: "swing"});
return false;
});
$("#div2Link").click(function() {
$("html, body").animate({
scrollTop: $("#div2").offset().top }, {duration: 500,easing: "swing"});
return false;
});
$("#div3Link").click(function() {
$("html, body").animate({
scrollTop: $("#div3").offset().top }, {duration: 500,easing: "swing"});
return false;
});
});
</script>
注意:運行上面的腳本的之前,先將為錨點增加相應(yīng)的id,同時去掉href屬性。
$("html, body")可以替換為響應(yīng)的div,如果不起作用,試著給該div增加overflow:scroll屬性。
另外,腳本可以進一步優(yōu)化,自己來試試
這樣做的好處是:URL地址不會變,同時點擊錨點時會自動響應(yīng)scroll事件,不需要重新綁定。
缺點是:如果頁面復(fù)雜的話,偏移值可能會發(fā)生變化需要算法輔助。
document.getElementById("divId").scrollIntoView();
比如:
document.querySelector("#roll1").onclick = function(){
document.querySelector("#roll1_top").scrollIntoView(true);
}
這里就是點擊id是#roll1的元素可以滾動到id是#roll1_top的地方,這里的#roll1和#roll1_top最好是一一對應(yīng)的,
這種方法的好處,是URL不會變,同時能夠響應(yīng)相應(yīng)的scroll事件,不需要算法什么的。代碼如下:
<html>
<head>
<title>HTML5_ScrollInToView方法</title>
<meta charset="utf-8">
<script type="text/javascript">
window.onload = function(){
/*
如果滾動頁面也是DOM沒有解決的一個問題。為了解決這個問題,瀏覽器實現(xiàn)了一下方法,
以方便開發(fā)人員如何更好的控制頁面的滾動。在各種專有方法中,HTML5選擇了scrollIntoView()
作為標準方法。
scrollIntoView()可以在所有的HTML元素上調(diào)用,通過滾動瀏覽器窗口或某個容器元素,
調(diào)用元素就可以出現(xiàn)在視窗中。如果給該方法傳入true作為參數(shù),或者不傳入任何參數(shù),那么
窗口滾動之后會讓調(diào)動元素頂部和視窗頂部盡可能齊平。如果傳入false作為參數(shù),調(diào)用元素
會盡可能全部出現(xiàn)在視口中(可能的話,調(diào)用元素的底部會與視口的頂部齊平。)不過頂部
不一定齊平,例如:
//讓元素可見
document.forms[0].scrollIntoView();
當頁面發(fā)生變化時,一般會用這個方法來吸引用戶注意力。實際上,為某個元素設(shè)置焦點也
會導(dǎo)致瀏覽器滾動顯示獲得焦點的元素。
支持該方法的瀏覽器有 IE、Firefox、Safari和Opera。
*/
document.querySelector("#roll1").onclick = function(){
document.querySelector("#roll_top").scrollIntoView(false);
}
document.querySelector("#roll2").onclick = function(){
document.querySelector("#roll_top").scrollIntoView(true);
}
}
</script>
<style type="text/css">
#myDiv{
height:900px;
background-color:gray;
}
#roll_top{
height:900px;
background-color:green;
color:#FFF;
font-size:50px;
position:relative;
}
#bottom{
position:absolute;
display:block;
left;0;bottom:0;
}
</style>
</head>
<body>
<button id="roll1">scrollIntoView(false)</button>
<button id="roll2">scrollIntoView(true)</button>
<div id="myDiv"></div>
<div id="roll_top">
scrollIntoView(ture)元素上邊框與視窗頂部齊平
<span id="bottom">scrollIntoView(false)元素下邊框與視窗底部齊平</span>
</div>
</body>
</html>
個人建議使用第四種方法。
一篇文章用VBA下載一部完整小說,VBA除了可以輕松下載一部《水滸傳》小說,還有什么用途?VBA獲取網(wǎng)頁信息(即爬蟲技術(shù))其用處可多了。今天介紹各位小伙用來監(jiān)控的自選股票的主力資金變動的情況,愿大伙在2022年股票市場長紅!也是把上期一個網(wǎng)友評論的內(nèi)容(有些數(shù)據(jù)藏得很深,怎么找真正的地址……)回應(yīng)一下。
東富的主力資金流入圖
有一個理論我們會經(jīng)常聽到,A股的票漲漲跌跌主要是因為有大資金在活動、在操作,一些股票不斷上漲是因為有主力資金不斷買入,反而不斷下跌是因為主力資金不斷流出。喜歡做短線的,我們實時觀察這些主力資金就變得很有意義,很多網(wǎng)站都有主力資金的數(shù)據(jù),但是要對多支股票進行觀察,則需要一個個打開網(wǎng)頁、APP或者電腦程序查看數(shù)據(jù),這樣耗時也不能多支股票同框?qū)Ρ取_€有一些小伙正上班,打開股票軟件給老板或同事看到,總覺得怪怪的,當然這不是鼓勵一些小伙上班玩股票,純屬用項目和大家一起交流學(xué)習(xí)。
用VBA就可以很輕松解決上述兩個問題。大伙先看看效果吧!
點擊按鈕更新數(shù)據(jù):2022年12月21日數(shù)據(jù)
完整代碼如下:
Sub maincapital()
Application.DisplayAlerts = False
'循環(huán)A列數(shù)據(jù),解析出相應(yīng)股票代碼
For Each cl In Range("a2:a" & Range("a2").End(xlDown).Row)
stock_code = Right(cl, 6) '取右邊6位字符
Dim strText, stcok_code As String
'拼接構(gòu)造網(wǎng)址
URL = "https://push2.eastmoney.com/api/qt/ulist.np/get?cb=fltt=2&secids=1." + stock_code + "&fields=f62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf64%2Cf65%2Cf70%2Cf71%2Cf76%2Cf77%2Cf82%2Cf83%2Cf164%2Cf166%2Cf168%2Cf170%2Cf172%2Cf252%2Cf253%2Cf254%2Cf255%2Cf256%2Cf124%2Cf6%2Cf278%2Cf279%2Cf280%2Cf281%2Cf282&ut=b2884a393a59ad64002292a3e90d46a5&_=1641268581776"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
strText = .responseText
End With
'判斷是有null,解決深A(yù)股和上A股網(wǎng)址不同拼接問題,secids=0.是深A(yù);secids=1.是上A。
Dim regStrTest As Object
With CreateObject("VBScript.Regexp")
.Pattern = "null"
Set regStrTest = .Execute(strText)
End With
If regStrTest.Count > 0 Then
'如果出現(xiàn)null則拼接構(gòu)造一個新網(wǎng)址
URL = "https://push2.eastmoney.com/api/qt/ulist.np/get?cb=fltt=2&secids=0." + stock_code + "&fields=f62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf64%2Cf65%2Cf70%2Cf71%2Cf76%2Cf77%2Cf82%2Cf83%2Cf164%2Cf166%2Cf168%2Cf170%2Cf172%2Cf252%2Cf253%2Cf254%2Cf255%2Cf256%2Cf124%2Cf6%2Cf278%2Cf279%2Cf280%2Cf281%2Cf282&ut=b2884a393a59ad64002292a3e90d46a5&_=1641268581776"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
strText = .responseText
End With
End If
Rnum = cl.Row '股票名稱所在行號
'利用正則表達式得到我們想要的內(nèi)容,并寫相應(yīng)的表格
With CreateObject("VBScript.Regexp")
.Pattern = """f62"":(.*?),"
Cells(Rnum, 2) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '主力凈流進
.Pattern = """f184"":(.*?),"
Cells(Rnum, 3) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '主力凈比
.Pattern = """f66"":(.*?),"
Cells(Rnum, 4) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '超大單凈流進
.Pattern = """f69"":(.*?),"
Cells(Rnum, 5) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '超大單凈比
.Pattern = """f72"":(.*?),"
Cells(Rnum, 6) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '大單凈流進
.Pattern = """f75"":(.*?),"
Cells(Rnum, 7) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '大單凈比
.Pattern = """f78"":(.*?),"
Cells(Rnum, 8) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '中單凈流進
.Pattern = """f81"":(.*?),"
Cells(Rnum, 9) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '中單凈比
.Pattern = """f84"":(.*?),"
Cells(Rnum, 10) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '小單凈流進
.Pattern = """f87"":(.*?),"
Cells(Rnum, 11) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '小單凈比
.Pattern = """f64"":(.*?),"
Cells(Rnum, 12) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '超大單流入
.Pattern = """f65"":(.*?),"
Cells(Rnum, 13) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '超大流出
.Pattern = """f70"":(.*?),"
Cells(Rnum, 14) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '大單流入
.Pattern = """f71"":(.*?),"
Cells(Rnum, 14) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '大單流出
End With
Next
Application.DisplayAlerts = True
End Sub
這個項目用到的幾個重要知識:
獲取動態(tài)網(wǎng)頁加載數(shù)據(jù)的真正網(wǎng)址
我們在目標網(wǎng)頁點擊右鍵,并點擊“查看網(wǎng)頁源代碼”后,當使用cttl+f鍵查看我們想要獲取的信息進行查看(這里我輸入主力凈比數(shù)據(jù)-42.76),發(fā)現(xiàn)并沒有相關(guān)的數(shù)據(jù)。
在源代碼網(wǎng)頁同時按cttl+f鍵查看
通過這個方法基本可以判斷目標網(wǎng)頁的部分數(shù)據(jù)是使用動態(tài)加載的方式。簡單的動態(tài)加載數(shù)據(jù),可以通過以下兩步獲取真正的網(wǎng)址:
第一步:打開瀏覽器控制臺。演示使用的瀏覽器是chrome,按下F12鍵打開瀏覽器控制臺,再次刷新網(wǎng)頁,在控制臺找到并點擊擴大鏡小按鈕,打開search對話框,這里輸入想找的數(shù)據(jù)(這里我輸入主力凈比數(shù)據(jù)42.76)后,按下回車鍵或輸入框旁邊的刷新按鍵,就會彈出一個“get”字符及一些類似網(wǎng)址的內(nèi)容,點擊彈出的內(nèi)容,右下面對話框會彈出相應(yīng)的內(nèi)容(對話框的菜單有Headers,Preview,Response……)
圖中主力凈比數(shù)據(jù)為-42.73和在輸入框的數(shù)據(jù)不一致,是因為資金數(shù)據(jù)動態(tài)變化了
第二步:找到加載數(shù)據(jù)的網(wǎng)址。點擊Headers可以看到Resquest URl字符,后面網(wǎng)址就是加載數(shù)據(jù)真正的網(wǎng)址,復(fù)制網(wǎng)址在瀏覽器打開,我們可以看到相應(yīng)的數(shù)據(jù)內(nèi)容。獲得這網(wǎng)址后,就可以通過VBA訪問這個網(wǎng)址獲取相應(yīng)的數(shù)據(jù),按下來的工作就好辦了。
目標數(shù)據(jù)的網(wǎng)址
使用CreateObject("MSXML2.XMLHTTP")對象獲取網(wǎng)頁信息
stock_code = Right(cl, 6) '取右邊6位字符
Dim strText, stcok_code As String '
'拼接構(gòu)造網(wǎng)址
URL = "https://push2.eastmoney.com/api/qt/ulist.np/get?cb=fltt=2&secids=1." + stock_code + "&fields=f62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf64%2Cf65%2Cf70%2Cf71%2Cf76%2Cf77%2Cf82%2Cf83%2Cf164%2Cf166%2Cf168%2Cf170%2Cf172%2Cf252%2Cf253%2Cf254%2Cf255%2Cf256%2Cf124%2Cf6%2Cf278%2Cf279%2Cf280%2Cf281%2Cf282&ut=b2884a393a59ad64002292a3e90d46a5&_=1641268581776"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
strText = .responseText
End With
'判斷是有null,解決深A(yù)股和上A股網(wǎng)址不同拼接問題,secids=0.是深A(yù);secids=1.是上A。
Dim regStrTest As Object
With CreateObject("VBScript.Regexp")
.Pattern = "null"
Set regStrTest = .Execute(strText)
End With
If regStrTest.Count > 0 Then
'如果出現(xiàn)null則拼接構(gòu)造一個新網(wǎng)址
URL = "https://push2.eastmoney.com/api/qt/ulist.np/get?cb=fltt=2&secids=0." + stock_code + "&fields=f62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf64%2Cf65%2Cf70%2Cf71%2Cf76%2Cf77%2Cf82%2Cf83%2Cf164%2Cf166%2Cf168%2Cf170%2Cf172%2Cf252%2Cf253%2Cf254%2Cf255%2Cf256%2Cf124%2Cf6%2Cf278%2Cf279%2Cf280%2Cf281%2Cf282&ut=b2884a393a59ad64002292a3e90d46a5&_=1641268581776"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
strText = .responseText
End With
End If
上面代碼的說明:
1、使用right(目標字符串,字符個數(shù))函數(shù)獲取目標字符串從右邊數(shù)起第幾個字符。
2、做兩次網(wǎng)址請求是因為深A(yù)股和上A股的數(shù)據(jù)網(wǎng)址是有區(qū)別,網(wǎng)址的參數(shù)“secids=0.+ 股票代碼”是深A(yù);“secids=1.+股票代碼”是上A。如果人為對每支股票判斷是上A還深A(yù)會很復(fù)雜,所以使用正則表達式,對請求數(shù)據(jù)內(nèi)容出現(xiàn)“null”字樣的網(wǎng)址,則更換網(wǎng)址參數(shù)。如下圖:
上A網(wǎng)址請求結(jié)果
深A(yù)網(wǎng)址請求結(jié)果
使用CreateObject("VBScript.Regexp")正則表達式對象獲取數(shù)據(jù)
For Each cl In Range("a2:a" & Range("a2").End(xlDown).Row)
……
Rnum = cl.Row '股票名稱所在行號
'利用正則表達式得到我們想要的內(nèi)容,并寫相應(yīng)的表格
With CreateObject("VBScript.Regexp")
.Pattern = """f62"":(.*?)," 'VBA的英文的雙引號
Cells(Rnum, 2) = Round(.Execute(strText)(0).submatches.Item(0) / 100000000, 2) '主力凈流入
.Pattern = """f184"":(.*?),"
Cells(Rnum, 3) = Round(.Execute(strText)(0).submatches.Item(0) / 100, 2) '主力凈比
.Pattern = """f66"":(.*?),"
……
上面代碼的說明:
1、Range("a2").End(xlDown)是表示定位從A2單元往下最后一個不空值的單元格。
2、cl.Row是表示單元格的行數(shù),Range("A1").Row,Cells(1, 1).Row都可以獲取單元格行數(shù),列值則是Range("A1").Column和Cells(1, 1).Column。
3、.Pattern = """f62"":(.*?)," 是正則表達式(網(wǎng)頁的內(nèi)容是:"f62":-15043563520.0),VBA的英文的雙引號用正則表式表示則是兩個雙引號,即是""。"f62":后面數(shù)據(jù)就是主力凈流入的數(shù)據(jù),用(.*?)表示。再用.submatches.Item(0)得到目標數(shù)據(jù),這個數(shù)據(jù)是以元為單位,網(wǎng)頁顯示的是億,所以要/100000000。
4、Round(目標數(shù)據(jù),小數(shù)點位數(shù))是取多少位小數(shù)的方法。
點擊按鈕調(diào)用函數(shù)
Private Sub CommandButton1_Click()
Call maincapital '調(diào)用函數(shù)
End Sub
在這總結(jié)一下VBA抓取數(shù)據(jù)主要步驟:
分析網(wǎng)站->>分析網(wǎng)頁數(shù)據(jù)->>處理網(wǎng)頁數(shù)據(jù)->>儲存網(wǎng)頁數(shù)據(jù)
歡迎大家評論及提意見,相互學(xué)習(xí),提高效率,創(chuàng)造價值。
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。