Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
EduSpider
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
viitto
EduSpider
Commits
ab0fdeae
Commit
ab0fdeae
authored
Jun 02, 2022
by
黄奎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1111
parent
db393ca0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
439 additions
and
0 deletions
+439
-0
SchoolHttpHelper.cs
EduSpider.Utility/SchoolHttpHelper.cs
+163
-0
AccountHelper.cs
EduSpider/Spiders/SchoolHouseKeeper/AccountHelper.cs
+140
-0
SchoolCourseManager.cs
EduSpider/Spiders/SchoolHouseKeeper/SchoolCourseManager.cs
+108
-0
SchoolTaskHelper.cs
EduSpider/Spiders/SchoolHouseKeeper/SchoolTaskHelper.cs
+28
-0
No files found.
EduSpider.Utility/SchoolHttpHelper.cs
0 → 100644
View file @
ab0fdeae
using
System
;
using
System.IO
;
using
System.Net
;
using
System.Net.Http
;
using
System.Text
;
using
VTX.FW.Helper
;
namespace
EduSpider.Utility
{
/// <summary>
/// Http请求帮助类
/// </summary>
public
class
SchoolHttpHelper
{
/// <summary>
/// 创建HttpClient
/// </summary>
/// <param name="cookie"></param>
/// <returns></returns>
public
static
HttpClient
GenerateHttp
(
string
cookie
)
{
var
handler
=
new
HttpClientHandler
{
AllowAutoRedirect
=
false
,
UseCookies
=
true
,
CookieContainer
=
CreateCookie
(
cookie
),
AutomaticDecompression
=
DecompressionMethods
.
GZip
,
ClientCertificateOptions
=
ClientCertificateOption
.
Automatic
};
var
http
=
new
HttpClient
(
handler
);
GenerateHttpHeader
(
ref
http
);
return
http
;
}
/// <summary>
/// 拼接Header参数
/// </summary>
/// <param name="http"></param>
private
static
void
GenerateHttpHeader
(
ref
HttpClient
http
)
{
http
.
DefaultRequestHeaders
.
Add
(
"Accept"
,
"*/*"
);
http
.
DefaultRequestHeaders
.
Add
(
"Accept-Encoding"
,
"gzip, deflate, br"
);
http
.
DefaultRequestHeaders
.
Add
(
"Accept-Language"
,
"zh-CN,zh;q=0.9"
);
//http.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
http
.
DefaultRequestHeaders
.
Add
(
"Cache-Control"
,
"no-cache"
);
http
.
DefaultRequestHeaders
.
Add
(
"Origin"
,
"https://tms11.xiaogj.com"
);
http
.
DefaultRequestHeaders
.
Add
(
"Pragma"
,
"no-cache"
);
http
.
DefaultRequestHeaders
.
Add
(
"Referer"
,
"https://tms11.xiaogj.com/index.html"
);
http
.
DefaultRequestHeaders
.
Add
(
"sec-ch-ua"
,
"\" Not; A Brand\";v=\"99\", \"Google Chrome\";v=\"97\", \"Chromium\";v=\"97\""
);
http
.
DefaultRequestHeaders
.
Add
(
"sec-ch-ua-mobile"
,
"?0"
);
http
.
DefaultRequestHeaders
.
Add
(
"sec-ch-ua-platform"
,
"\"Windows\""
);
http
.
DefaultRequestHeaders
.
Add
(
"Sec-Fetch-Dest"
,
"empty"
);
http
.
DefaultRequestHeaders
.
Add
(
"Sec-Fetch-Mode"
,
"cors"
);
http
.
DefaultRequestHeaders
.
Add
(
"Sec-Fetch-Site"
,
"same-origin"
);
http
.
DefaultRequestHeaders
.
Add
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
);
http
.
DefaultRequestHeaders
.
Add
(
"pragma"
,
"no-cache"
);
}
/// <summary>
/// 拼接Cookie
/// </summary>
/// <param name="cookieStr"></param>
/// <returns></returns>
private
static
CookieContainer
CreateCookie
(
string
cookieStr
)
{
var
uri
=
new
Uri
(
"https://tms11.xiaogj.com"
);
var
cc
=
new
CookieContainer
();
foreach
(
var
str
in
cookieStr
.
Split
(
';'
))
{
cc
.
SetCookies
(
uri
,
str
);
}
return
cc
;
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="url">url地址</param>
/// <param name="body">参数</param>
/// <param name="contenttype">参数</param>
/// <returns></returns>
public
static
string
HttpPost
(
string
url
,
string
body
,
string
contenttype
=
""
,
string
cookie
=
""
)
{
try
{
Encoding
encoding
=
Encoding
.
UTF8
;
HttpWebRequest
request
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
request
.
Method
=
"POST"
;
request
.
Accept
=
"application/json, text/javascript, */*"
;
//"text/html, application/xhtml+xml, */*";
request
.
ContentType
=
"application/json; charset=utf-8"
;
if
(
contenttype
!=
""
)
{
request
.
ContentType
=
contenttype
;
}
if
(!
string
.
IsNullOrEmpty
(
cookie
))
{
request
.
Headers
.
Add
(
"cookie"
,
cookie
);
}
byte
[]
buffer
=
encoding
.
GetBytes
(
body
);
request
.
ContentLength
=
buffer
.
Length
;
request
.
GetRequestStream
().
Write
(
buffer
,
0
,
buffer
.
Length
);
HttpWebResponse
response
=
(
HttpWebResponse
)
request
.
GetResponse
();
using
StreamReader
reader
=
new
StreamReader
(
response
.
GetResponseStream
(),
encoding
);
return
reader
.
ReadToEnd
();
}
catch
(
WebException
ex
)
{
var
res
=
(
HttpWebResponse
)
ex
.
Response
;
StringBuilder
sb
=
new
StringBuilder
();
StreamReader
sr
=
new
StreamReader
(
res
.
GetResponseStream
(),
Encoding
.
UTF8
);
sb
.
Append
(
sr
.
ReadToEnd
());
throw
new
Exception
(
sb
.
ToString
());
}
}
/// <summary>
/// 执行HTTP GET请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <returns>HTTP响应</returns>
public
static
string
HttpGet
(
string
url
)
{
string
result
=
""
;
HttpWebRequest
req
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
req
.
Method
=
"GET"
;
req
.
ContentType
=
"application/json"
;
HttpWebResponse
rsp
=
(
HttpWebResponse
)
req
.
GetResponse
();
Encoding
encoding
=
Encoding
.
UTF8
;
if
(!
string
.
IsNullOrWhiteSpace
(
rsp
.
CharacterSet
))
{
encoding
=
Encoding
.
GetEncoding
(
rsp
.
CharacterSet
);
}
System
.
IO
.
Stream
stream
=
null
;
StreamReader
reader
=
null
;
try
{
// 以字符流的方式读取HTTP响应
stream
=
rsp
.
GetResponseStream
();
reader
=
new
StreamReader
(
stream
,
encoding
);
result
=
reader
.
ReadToEnd
();
}
catch
(
Exception
ex
)
{
LogHelper
.
WriteError
(
""
,
string
.
Format
(
"HttpGet:url=={0}"
,
url
),
ex
);
result
=
""
;
}
finally
{
// 释放资源
if
(
reader
!=
null
)
reader
.
Close
();
if
(
stream
!=
null
)
stream
.
Close
();
if
(
rsp
!=
null
)
rsp
.
Close
();
}
return
result
;
}
}
}
\ No newline at end of file
EduSpider/Spiders/SchoolHouseKeeper/AccountHelper.cs
0 → 100644
View file @
ab0fdeae
using
OpenQA.Selenium
;
using
OpenQA.Selenium.Chrome
;
using
System
;
using
System.Net
;
using
System.Net.Http
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
EduSpider.Spiders.SchoolHouseKeeper
{
/// <summary>
/// 校管家
/// </summary>
public
class
AccountHelper
{
private
static
readonly
object
lockerSafe
=
new
();
private
static
string
_loginCookies
=
""
;
private
static
bool
_openHeartTimer
=
false
;
public
static
string
GetInstance
()
{
if
(
string
.
IsNullOrEmpty
(
_loginCookies
))
{
lock
(
lockerSafe
)
{
if
(
string
.
IsNullOrEmpty
(
_loginCookies
))
{
_loginCookies
=
string
.
Empty
;
var
cnt
=
0
;
while
(
cnt
<
4
)
{
cnt
++;
var
result
=
RunAsync
();
if
(
result
&&
!
_openHeartTimer
)
{
_openHeartTimer
=
true
;
Task
.
Run
(()
=>
HeartLine
());
break
;
}
else
{
Console
.
WriteLine
(
"开始重新尝试登录校管家"
);
Thread
.
Sleep
(
2000
);
}
}
}
}
}
return
_loginCookies
;
}
public
static
bool
RunAsync
()
{
var
flag
=
false
;
IWebElement
inputEle
;
IWebElement
pwdEle
;
var
option
=
new
ChromeOptions
();
option
.
AddArgument
(
"--incognito"
);
option
.
AddArgument
(
"headless"
);
option
.
AddArgument
(
"disable-infobars"
);
option
.
AddArgument
(
"window-size=1920,1080"
);
option
.
AddArgument
(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
);
ChromeDriverService
service
=
ChromeDriverService
.
CreateDefaultService
(
Environment
.
CurrentDirectory
);
service
.
HideCommandPromptWindow
=
true
;
option
.
PageLoadStrategy
=
PageLoadStrategy
.
Eager
;
IWebDriver
driver
=
new
ChromeDriver
(
service
,
option
);
try
{
driver
.
Navigate
().
GoToUrl
(
$"https://tms.xiaogj.com/login.aspx"
);
driver
.
Manage
().
Timeouts
().
PageLoad
=
TimeSpan
.
FromSeconds
(
15
);
inputEle
=
driver
.
FindElement
(
By
.
Id
(
"name"
));
pwdEle
=
driver
.
FindElement
(
By
.
Id
(
"pwd"
));
if
(
inputEle
!=
null
&&
pwdEle
!=
null
)
{
inputEle
.
SendKeys
(
"康磊@jjsw"
);
pwdEle
.
SendKeys
(
"jjswchemkl2020"
);
var
buttonEle
=
driver
.
FindElement
(
By
.
Id
(
"loginBtn"
));
Thread
.
Sleep
(
1000
);
//开始登录
buttonEle
.
Click
();
//处理
foreach
(
var
item
in
driver
.
Manage
().
Cookies
.
AllCookies
)
{
_loginCookies
+=
item
.
Name
+
"="
+
item
.
Value
+
";"
;
}
_loginCookies
=
_loginCookies
.
Substring
(
0
,
_loginCookies
.
Length
-
1
);
flag
=
true
;
}
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
$"获取cooki失败,
{
ex
.
Message
}
"
);
}
driver
.
Quit
();
Console
.
WriteLine
(
"完成登录..."
);
return
flag
;
}
private
static
void
HeartLine
()
{
while
(
_openHeartTimer
)
{
var
handler
=
new
HttpClientHandler
{
AllowAutoRedirect
=
false
,
UseCookies
=
true
,
CookieContainer
=
CreateCookie
(),
AutomaticDecompression
=
DecompressionMethods
.
GZip
,
ClientCertificateOptions
=
ClientCertificateOption
.
Automatic
};
var
http
=
new
HttpClient
(
handler
);
http
.
GetAsync
(
"https://tms11.xiaogj.com/"
);
//TODO:检查是否需要重新登录
Thread
.
Sleep
(
60
*
1000
);
}
}
public
static
void
StopHeartLine
()
{
_openHeartTimer
=
false
;
}
private
static
CookieContainer
CreateCookie
()
{
var
uri
=
new
Uri
(
"https://tms11.xiaogj.com"
);
var
cc
=
new
CookieContainer
();
foreach
(
var
str
in
_loginCookies
.
Split
(
';'
))
{
cc
.
SetCookies
(
uri
,
str
);
}
return
cc
;
}
}
}
EduSpider/Spiders/SchoolHouseKeeper/SchoolCourseManager.cs
0 → 100644
View file @
ab0fdeae
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Text
;
using
System.Threading.Tasks
;
using
VTX.FW.Helper
;
namespace
EduSpider.Spiders.SchoolHouseKeeper
{
/// <summary>
/// 校管家课程关联
/// </summary>
public
class
SchoolCourseManager
{
public
class
PageModel
{
public
int
PageIndex
{
get
;
set
;
}
public
int
PageSize
{
get
;
set
;
}
}
/// <summary>
/// 获取课程列表
/// </summary>
public
static
async
void
RunCourse
(
string
cookie
)
{
var
pageModel
=
new
PageModel
{
PageIndex
=
1
,
PageSize
=
10
};
int
totalCount
=
0
,
pageCount
=
0
;
string
url
=
"https://tms11.xiaogj.com/api/Shift/Query"
;
string
result
=
Utility
.
SchoolHttpHelper
.
HttpPost
(
url
,
VTX
.
FW
.
Helper
.
JsonHelper
.
Serialize
(
pageModel
),
"application/json"
,
cookie
);
List
<
SchoolCourseItem
>
list
=
new
List
<
SchoolCourseItem
>();
if
(!
string
.
IsNullOrEmpty
(
result
))
{
JObject
jobj
=
JObject
.
Parse
(
result
);
totalCount
=
jobj
.
GetInt
(
"TotalCount"
);
pageCount
=
jobj
.
GetInt
(
"PageCount"
);
list
=
ParseJson
(
jobj
.
GetString
(
"Data"
));
if
(
pageCount
>
pageModel
.
PageIndex
)
{
for
(
var
i
=
2
;
i
<=
pageCount
;
i
++)
{
}
}
}
Console
.
WriteLine
(
"result:"
+
result
);
}
/// <summary>
/// 解析JSON
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
List
<
SchoolCourseItem
>
ParseJson
(
string
data
)
{
List
<
SchoolCourseItem
>
list
=
new
List
<
SchoolCourseItem
>();
if
(!
string
.
IsNullOrEmpty
(
data
))
{
JArray
courseArray
=
JArray
.
Parse
(
data
);
if
(
courseArray
!=
null
&&
courseArray
.
Count
>
0
)
{
foreach
(
var
item
in
courseArray
)
{
JObject
cObj
=
JObject
.
Parse
(
item
.
ToString
());
var
model
=
new
SchoolCourseItem
()
{
ID
=
cObj
.
GetString
(
"ID"
),
Name
=
cObj
.
GetString
(
"Name"
),
IsOneToOne
=
cObj
.
GetInt
(
"IsOneToOne"
),
};
list
.
Add
(
model
);
}
}
}
return
list
;
}
}
/// <summary>
/// 校管家课程实体
/// </summary>
public
class
SchoolCourseItem
{
/// <summary>
/// 课程编号GUID
/// </summary>
public
string
ID
{
get
;
set
;
}
/// <summary>
/// 课程名称
/// </summary>
public
string
Name
{
get
;
set
;
}
/// <summary>
/// 是否一对一
/// </summary>
public
int
IsOneToOne
{
get
;
set
;
}
}
}
EduSpider/Spiders/SchoolHouseKeeper/SchoolTaskHelper.cs
0 → 100644
View file @
ab0fdeae
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
EduSpider.Spiders.SchoolHouseKeeper
{
/// <summary>
/// 校管家任务
/// </summary>
public
class
SchoolTaskHelper
{
/// <summary>
/// 校管家任务
/// </summary>
public
static
void
RunTask
()
{
Console
.
WriteLine
(
"开始校管家任务"
);
string
cookies
=
""
;
//cookies = AccountHelper.GetInstance();
cookies
=
"_gat_gtag_UA_167872829_1=1;_ga=GA1.2.1031016650.1654154573;_gid=GA1.2.876560535.1654154573;.ASPXAUTH=056D300F98E85276C3ED92CF49A02A66944CE75D5F4EE765A81B0F96357EFB5CF2E42BFB639F4F22A5CA94B2CC2723996E8FAD31912D2F7FDFE8819F53E8CE9806444EA3D590C276C6990E79FDD778A8D6300E7B80858646F82D930DF1BA7A6E936ED82C13B77EE0462949883B03C894359F685FEF44A6390DD0945BD1DDCC715B93E256711F67AF113149AAA6042266;ASP.NET_SessionId=ips0xoryxjnueqxbebttinpt"
;
SchoolCourseManager
.
RunCourse
(
cookies
);
Console
.
WriteLine
(
"结束校管家任务"
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment