使用Post方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法
最近弄爬蟲,遇到的一個問題就是如何使用post方法模擬登陸爬取網(wǎng)頁。
下面是極簡版的代碼:

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請求地址
private static final String POST_URL = "";
//模擬谷歌瀏覽器請求
private static final String USER_AGENT = "";
//用賬號登錄某網(wǎng)站后 請求POST_URL鏈接獲取cookie
private static final String COOKIE = "";
//用賬號登錄某網(wǎ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請求
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;
}
}
以上這篇使用Post方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java保證對象在內(nèi)存中唯一性的實(shí)現(xiàn)方法
這篇文章主要介紹了java如何保證對象在內(nèi)存中的唯一性,如果創(chuàng)建多個對象的話,可能會引發(fā)出各種各樣的問題,這時,就需要我們保證這個對象在內(nèi)存中的唯一性,需要的朋友可以參考下2019-06-06
SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
這篇文章主要介紹了SpringBoot整合Mybatis實(shí)現(xiàn)CRUD的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Java中關(guān)鍵字final finally finalize的區(qū)別介紹
這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考的價值,需要的朋友可以參考一下2022-04-04
關(guān)于Java中修飾符的總結(jié)(fina除外)
下面小編就為大家?guī)硪黄P(guān)于Java中修飾符的總結(jié)(fina除外)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
SpringBoot實(shí)現(xiàn)全局異常的封裝和統(tǒng)一處理
在Spring Boot應(yīng)用中,全局異常的處理是一個非常重要的方面,本文主要為大家詳細(xì)介紹了如何在Spring Boot中進(jìn)行全局異常的封裝和統(tǒng)一處理,需要的可以參考下2023-12-12
java使用分隔符連接數(shù)組中每個元素的實(shí)例
今天小編就為大家分享一篇java使用分隔符連接數(shù)組中每個元素的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05

