日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【爬虫】爬取带有cookie才能获取网页内容的新闻网站

發(fā)布時(shí)間:2024/8/23 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【爬虫】爬取带有cookie才能获取网页内容的新闻网站 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

工作任務(wù):

今天老大讓我跑取一個(gè)新聞網(wǎng)站:https://www.yidaiyilu.gov.cn/

采坑記錄:

  • https協(xié)議,如果利用http協(xié)議去請(qǐng)求會(huì)報(bào)出如下信息:

錯(cuò)誤:SSLHandshake錯(cuò)誤就知道了,客戶端與服務(wù)端進(jìn)行連接時(shí),需要通過(guò)SSL協(xié)議進(jìn)行握手

(坑)改用:重寫(xiě)DefaultHttpClient方法使其支持SSL協(xié)議

?

package httpsParse; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; //用于進(jìn)行Https請(qǐng)求的HttpClient public class SSLClient extends DefaultHttpClient{ public SSLClient() throws Exception{ super(); //傳輸協(xié)議需要根據(jù)自己的判斷  SSLContext ctx = SSLContext.getInstance("TLSv1.2"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } }

(坑)然后再利用HttpClient去請(qǐng)求獲取網(wǎng)頁(yè)源代碼:

public static void main(String[] args) throws Exception {HttpClientUtil httpClientUtil = new HttpClientUtil();String url = "https://www.yidaiyilu.gov.cn/zchj.htm";String html = httpClientUtil.doGet(url);System.out.println(html);}

?

最后發(fā)現(xiàn)得到的結(jié)果:是一段js代碼

<script>var x="@catch@@@d@@toString@@String@@36@pathname@if@@toLowerCase@var@855@captcha@@Array@@@1@@for@1500@@document@@@@chars@attachEvent@addEventListener@substr@Expires@@false@f@0@fromCharCode@innerHTML@@@@8@@@@@@@split@parseInt@createElement@g@new@16@search@May@@https@@reverse@@RegExp@@while@@@charCodeAt@rOm9XFMtA3QKV7nYsPGT4lifyWwkq5vcjH2IdxUoCbhERLaz81DNB6@@10@JgSe0upZ@else@match@0xFF@@@07@length@@e@eval@@@19@@@Path@a@div@setTimeout@cookie@3@5@@0xEDB88320@@GMT@challenge@@@Tue@@@window@@href@return@try@@@@@location@onreadystatechange@function@1557242170@DOMContentLoaded@@firstChild@replace@__jsl_clearance@charAt@join@".replace(/@*$/,"").split("@"),y="g 3b=3q(){31('3o.3h=3o.c+3o.1s.40(/[\\?|&]i-39/,\\'\\')',q);s.32='41=3r.h|19|'+(3q(){g 1i=[3q(3b){3i 2n('9.1a('+3b+')')},(3q(){g 3b=s.1o('30');3b.1b='<2u 3h=\\'/\\'>3l</2u>';3b=3b.3u.3h;g 1i=3b.2f(/20?:\\/\\//)[19];3b=3b.14(1i.2k).f();3i 3q(1i){p(g 3l=19;3l<1i.2k;3l++){1i[3l]=3b.42(1i[3l])};3i 1i.43('')}})()],3l=[[([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])+[-~~~!{}+[~~[]]-(-~~~!{})],(-~{}+[]+[[]][19])+[~~'']+[-~(+!+{})],[34]+(-~[-~{}-~{}]+[[]][19]),[-~{}-~[-~{}-~{}]]+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19]),(-~{}+[]+[[]][19])+(-~{}+[]+[[]][19])+[-~(+!+{})],(-~{}+[]+[[]][19])+(-~{}+[]+[[]][19])+[-~{}-~[-~{}-~{}]],[33-~(+!+{})-~(+!+{})]+(-~[-~{}-~{}]+[[]][19]),[34]+[-~(+!+{})],[-~~~!{}+[~~[]]-(-~~~!{})]+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19]),[33-~(+!+{})-~(+!+{})]+(-~[-~{}-~{}]+[[]][19]),(-~{}+[]+[[]][19])+[~~'']+[33-~(+!+{})-~(+!+{})]],[(-~{}+[]+[[]][19])+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19]),[33-~(+!+{})-~(+!+{})]],[[34]+[-~{}-~[-~{}-~{}]],(-~[-~{}-~{}]+[[]][19])+[33-~(+!+{})-~(+!+{})],[34]+[~~''],([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])+([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[]),([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19]),(-~{}+[]+[[]][19])+(-~{}+[]+[[]][19])+[34]],[(-~{}+[]+[[]][19])+[-~(+!+{})],([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])],[[34]+(-~{}+[]+[[]][19]),(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19])+[~~''],[34]+[34],[34]+([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[]),[-~{}-~[-~{}-~{}]]+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19])],[(-~{}+[]+[[]][19])+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19]),(-~{}+[]+[[]][19])+[-~(+!+{})]],[([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])+[-~~~!{}+[~~[]]-(-~~~!{})],(-~[-~{}-~{}]+[[]][19])+[33-~(+!+{})-~(+!+{})],[34]+(-~{}+[]+[[]][19]),([(-~[]<<-~[])]*(((+!+{})+[(-~[]<<-~[])]>>(-~[]<<-~[])))+[])+(((-~[]<<-~[])<<(-~[]<<-~[]))+[[]][19])]];p(g 3b=19;3b<3l.2k;3b++){3l[3b]=1i.22()[(-~{}+[]+[[]][19])](3l[3b])};3i 3l.43('')})()+';15=3c, 2j-1t-2q 1r:1r:2c 38;2t=/;'};d((3q(){3j{3i !!3f.13;}2(2m){3i 17;}})()){s.13('3s',3b,17)}2e{s.12('3p',3b)}",f=function(x,y){var a=0,b=0,c=0;x=x.split("");y=y||99;while((a=x.shift())&&(b=a.charCodeAt(0)-77.5))c=(Math.abs(b)<13?(b+48.5):parseInt(a,36))+y*c;return c},z=f(y.match(/\w/g).sort(function(x,y){return f(x)-f(y)}).pop());while(z++)try{eval(y.replace(/\b\w+\b/g, function(y){return x[f(y,z)-1]||("_"+y)}));break}catch(_){}</script>
  • 開(kāi)始懷疑是cookie的原因,然后在瀏覽器中將cookie帶上去最后請(qǐng)求出結(jié)果,但是cookie是有有效期的,隔一段時(shí)間cookie就失效了,因此這種方法行不通
  • 后來(lái)分析發(fā)現(xiàn)瀏覽器訪問(wèn)該網(wǎng)站首先會(huì)加載js然后生成cookie,再將這次生成的cookie帶上請(qǐng)求頭再次請(qǐng)求,所以為什么第一次上面的請(qǐng)求會(huì)出現(xiàn)js代碼,但是js是動(dòng)態(tài)加載的,因此需要利用java模擬瀏覽的方式去實(shí)現(xiàn)
  • 最終通過(guò)htmlunit實(shí)現(xiàn)的代碼:
package cn.server;import org.openqa.selenium.htmlunit.HtmlUnitDriver;public class GFDynamicWeb {public static HtmlUnitDriver driver = new HtmlUnitDriver();public static boolean isGetCookie = false; // public static boolean isRepeatExec = false;public static String GetContent(String url) {if(!isGetCookie) {driver.setJavascriptEnabled(true);//第一次加載js獲取cookiedriver.get(url);}driver.setJavascriptEnabled(false);//第二次加載網(wǎng)頁(yè)源碼driver.get(url);String pageSource = driver.getPageSource();isGetCookie = true;return pageSource;}public static void renewIsGetCookie() {isGetCookie = false;}public static void closeDriver() {driver.close();}public static void main(String[] args) {long s = System.currentTimeMillis();for(int i = 0; i < 100; i ++) {String url = "https://www.yidaiyilu.gov.cn/";String content = GetContent(url);System.out.println(content);}long e = System.currentTimeMillis();System.out.println((e - s)/1000 + "秒");renewIsGetCookie();closeDriver();} }

期間利用的網(wǎng)址:

在線接口測(cè)試

521狀態(tài)碼作用

521出錯(cuò)問(wèn)題解決辦法

總結(jié)

以上是生活随笔為你收集整理的【爬虫】爬取带有cookie才能获取网页内容的新闻网站的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。