SeimiAgent使用--經過js控制以瀏覽器級效果登錄爬取京東信息

聲明

本篇文章純粹爲了向還不是很瞭解SeimiAgent的同窗演示下SeimiAgent的部分能力,目標網站隨意選的,並無其餘目的。html

SeimiAgent簡介

SeimiAgent是基於QtWebkit開發的可在服務器端後臺運行的一個webkit服務,能夠經過SeimiAgent提供的http接口向SeimiAgent發送一個load請求(需求加載的URL以及對這個頁面接受的渲染時間或是使用什麼代理等參數),經過SeimiAgent去加載並渲染想要處理的動態頁面,而後將渲染好的頁面直接返給調用方進行後續處理,因此運行的SeimiAgent服務是與語言無關的,任何一種語言或框架均可以經過SeimiAgent提供的標準http接口來獲取服務。SeimiAgent的加載渲染環境都是通用瀏覽器級的,因此不用擔憂他對動態頁面的處理能力。同時支持渲染生成頁面快照(png)和PDF,亦支持自定義js腳本處理基本渲染後的頁面,具體請參見官方使用說明。java

正文

視頻分享

爲了更爲直觀的瞭解,能夠先看下分享在優酷上視頻,點擊地址直達python

下面會是圖片版的詳細介紹git

啓動SeimiAgent

在SeimiAgent的目錄下:github

./bin/seimiagent -p 8000

啓動SeimiAgent

登錄京東(JD)

向SeimiAgent發送登錄請求,演示中爲了方便直接使用了curl與SeimiAgent進行交互。因爲SeimiAgent接受標準的http指令,因此能夠用任何語言來控制他。web

  • curl
curl -X POST -H "Accept-Charset: UTF-8" -H "Cache-Control: no-cache" -H "Postman-Token: 017ba6d3-8b1a-872e-88eb-ea663ce16313" -H "Content-Type: application/x-www-form-urlencoded" -d 'url=https://passport.jd.com/uc/login&renderTime=6000&script=$("#loginname").val("seimimaster");$("#nloginpwd").val("xxxxx");$(".login-btn>a").click();&contentType=img&useCookie=1' "http://localhost:8000/doload" -o login_jd.png

發送登錄指令

這裏是告訴SeimiAgent啓用cookie,並使用一段自定義的JavaScript腳原本控制頁面進行登錄,並以圖片的形式輸出渲染結果(爲了方便給你們看)。結果頁面以下(文章篇幅有限文章內部使用時進行了動態截斷,全圖能夠自行新窗口打開,去掉鏈接中?後的參數):瀏覽器

登錄結果

經過頭部能夠看出登錄成功服務器

其餘語言與SeimiAgent交互示例cookie

  • java(okhttp)
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "url=https%3A%2F%2Fpassport.jd.com%2Fuc%2Flogin&renderTime=6000&script=%24(%22%23loginname%22).val(%22seimimaster%22)%3B%24(%22%23nloginpwd%22).val(%22seimi%22)%3B%24(%22.login-btn%3Ea%22).click()%3B&contentType=img&useCookie=1");
Request request = new Request.Builder()
  .url("http://localhost:8000/doload")
  .post(body)
  .addHeader("accept-charset", "UTF-8")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "96caaa7b-3978-9a94-cd15-af884aa19bb4")
  .addHeader("content-type", "application/x-www-form-urlencoded")
  .build();

Response response = client.newCall(request).execute();
  • python
import requests

url = "http://localhost:8000/doload"

payload = "url=https%3A%2F%2Fpassport.jd.com%2Fuc%2Flogin&renderTime=6000&script=%24(%22%23loginname%22).val(%22seimimaster%22)%3B%24(%22%23nloginpwd%22).val(%22seimi%22)%3B%24(%22.login-btn%3Ea%22).click()%3B&contentType=img&useCookie=1"
headers = {
    'accept-charset': "UTF-8",
    'cache-control': "no-cache",
    'postman-token': "17135568-b9d5-97d6-f24f-deaa3877b0c0",
    'content-type': "application/x-www-form-urlencoded"
    }

response = requests.request("POST", url, data=payload, headers=headers)
  • Nodejs
var request = require("request");

var options = { method: 'POST',
  url: 'http://localhost:8000/doload',
  headers: 
   { 'content-type': 'application/x-www-form-urlencoded',
     'postman-token': '6d1bc037-3f74-6a2c-d3da-426e2070bc5a',
     'cache-control': 'no-cache',
     'accept-charset': 'UTF-8' },
  form: 
   { url: 'https://passport.jd.com/uc/login',
     renderTime: '6000',
     script: '$("#loginname").val("seimimaster");$("#nloginpwd").val("seimi");$(".login-btn>a").click();',
     contentType: 'img',
     useCookie: '1' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);
  //body 爲圖片文件流,自行處理
});
  • go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)
func main() {
	url := "http://localhost:8000/doload"
	payload := strings.NewReader("url=https%3A%2F%2Fpassport.jd.com%2Fuc%2Flogin&renderTime=6000&script=%24(%22%23loginname%22).val(%22seimimaster%22)%3B%24(%22%23nloginpwd%22).val(%22seimi%22)%3B%24(%22.login-btn%3Ea%22).click()%3B&contentType=img&useCookie=1")
	req, _ := http.NewRequest("POST", url, payload)
	req.Header.Add("accept-charset", "UTF-8")
	req.Header.Add("cache-control", "no-cache")
	req.Header.Add("postman-token", "dd2d6df6-15a3-29b2-2431-b323e00de490")
	req.Header.Add("content-type", "application/x-www-form-urlencoded")
	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

}
  • C#
var client = new RestClient("http://localhost:8000/doload");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("postman-token", "614dc816-370b-ac55-097e-e581ddac601c");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("accept-charset", "UTF-8");
request.AddParameter("application/x-www-form-urlencoded", "url=https%3A%2F%2Fpassport.jd.com%2Fuc%2Flogin&renderTime=6000&script=%24(%22%23loginname%22).val(%22seimimaster%22)%3B%24(%22%23nloginpwd%22).val(%22seimi%22)%3B%24(%22.login-btn%3Ea%22).click()%3B&contentType=img&useCookie=1", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

訪問我的信息頁

在登錄後繼續訪問我的信息頁,驗證cookie的連續性。app

curl -X POST -H "Accept-Charset: UTF-8" -H "Cache-Control: no-cache" -H "Postman-Token: 6a6c9ae9-1b18-7c02-d1fb-7506a9e79549" -H "Content-Type: application/x-www-form-urlencoded" -d 'url=https://home.jd.com/&renderTime=3000&contentType=img&useCookie=1' "http://localhost:8000/doload" -o profile_jd.png

我的信息頁

獲取結果以下(文章篇幅有限文章內部使用時進行了動態截斷,全圖能夠自行新窗口打開,去掉鏈接中?後的參數):

我的信息頁結果

能夠看到,cookie是延續有效的。

總結

經過上面,能夠看到讓SeimiAgent只經過一條很是簡單的JavaScript腳本便完成了京東這種複雜系統的登錄以及登錄後複雜的動態頁面渲染。

因此,大家懂得

來github給SeimiAgent盡情的砸star吧。

項目地址

https://github.com/zhegexiaohuozi/SeimiAgent

相關文章
相關標籤/搜索