創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!
為灞橋等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及灞橋網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站制作、灞橋網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!最近弄爬蟲(chóng),遇到的一個(gè)問(wèn)題就是如何使用post方法模擬登陸爬取網(wǎng)頁(yè)。
下面是極簡(jiǎn)版的代碼:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; public class test { //post請(qǐng)求地址 private static final String POST_URL = ""; //模擬谷歌瀏覽器請(qǐng)求 private static final String USER_AGENT = ""; //用賬號(hào)登錄某網(wǎng)站后 請(qǐng)求POST_URL鏈接獲取cookie private static final String COOKIE = ""; //用賬號(hào)登錄某網(wǎng)站后 請(qǐng)求POST_URL鏈接獲取數(shù)據(jù)包 private static final String REQUEST_DATA = ""; public static void main(String[] args) throws Exception { HashMap<String, String> map = postCapture(REQUEST_DATA); String responseCode = map.get("responseCode"); String value = map.get("value"); while(!responseCode.equals("200")){ map = postCapture(REQUEST_DATA); responseCode = map.get("responseCode"); value = map.get("value"); } //打印爬取結(jié)果 System.out.println(value); } private static HashMap<String, String> postCapture(String requestData) throws Exception{ HashMap<String, String> map = new HashMap<>(); URL url = new URL(POST_URL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setDoInput(true); // 設(shè)置輸入流采用字節(jié)流 httpConn.setDoOutput(true); // 設(shè)置輸出流采用字節(jié)流 httpConn.setUseCaches(false); //設(shè)置緩存 httpConn.setRequestMethod("POST");//POST請(qǐng)求 httpConn.setRequestProperty("User-Agent", USER_AGENT); httpConn.setRequestProperty("Cookie", COOKIE); PrintWriter out = new PrintWriter(new OutputStreamWriter(httpConn.getOutputStream(), "UTF-8")); out.println(requestData); out.close(); int responseCode = httpConn.getResponseCode(); StringBuffer buffer = new StringBuffer(); if (responseCode == 200) { BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); httpConn.disconnect(); } map.put("responseCode", new Integer(responseCode).toString()); map.put("value", buffer.toString()); return map; } }
網(wǎng)頁(yè)標(biāo)題:使用Post方法模擬登陸爬取網(wǎng)頁(yè)的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.rwnh.cn/article8/pipip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、定制網(wǎng)站、微信公眾號(hào)、電子商務(wù)、ChatGPT、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容