Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
马玮
/
carspider
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
75796e98
authored
Aug 10, 2017
by
马玮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
easypass ended
parent
199080ab
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
340 additions
and
7 deletions
+340
-7
README.md
+2
-3
lib/dealerspider/autohome.js
+4
-3
lib/dealerspider/easypass.js
+334
-1
No files found.
README.md
View file @
75796e98
...
...
@@ -17,5 +17,4 @@ Installation
To do list:
------
1.
Add random delay when access a page
2.
Add error stack
\ No newline at end of file
1.
Fetch easypass clues
\ No newline at end of file
lib/dealerspider/autohome.js
View file @
75796e98
...
...
@@ -101,7 +101,7 @@ class AutoHome {
}
catch
(
e
)
{
console
.
log
(
e
.
message
);
}
console
.
log
(
'
成功就不走这
'
);
console
.
log
(
'
开始表单登录
'
);
const
username
=
this
.
params
.
username
;
const
password
=
this
.
params
.
password
;
...
...
@@ -114,7 +114,7 @@ class AutoHome {
horseman
.
userAgent
(
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
)
.
viewport
(
1920
,
1080
)
.
open
(
'http://ics.autohome.com.cn/passport/
Home/Index
'
);
.
open
(
'http://ics.autohome.com.cn/passport/'
);
horseman
.
on
(
'onResourceRequested'
,
(
requestData
,
networkRequest
)
=>
{
console
.
info
(
'Requesting'
,
requestData
.
url
);
...
...
@@ -164,6 +164,7 @@ class AutoHome {
.
waitForNextPage
({
timeout
:
5000
})
.
screenshot
(
'm3.png'
)
.
exists
(
'#loginout'
);
if
(
loginSuccess
)
{
console
.
log
(
'登录成功!'
);
const
cookies
=
await
horseman
.
cookies
().
close
();
...
...
@@ -202,7 +203,7 @@ class AutoHome {
const
Cookie
=
tough
.
Cookie
;
cookies
.
forEach
((
cookieObj
)
=>
{
let
cookieStr
=
cookieObj
.
name
+
"="
+
cookieObj
.
value
+
"; path="
+
cookieObj
.
path
+
"; domain="
+
cookieObj
.
domain
;
let
cookieStr
=
cookieObj
.
name
+
'='
+
cookieObj
.
value
+
'; path='
+
cookieObj
.
path
+
'; domain='
+
cookieObj
.
domain
;
cookieJar
.
setCookieSync
(
cookieStr
,
'http://ics.autohome.com.cn'
);
});
...
...
lib/dealerspider/easypass.js
View file @
75796e98
'use strict'
;
process
.
env
.
NODE_CONFIG_DIR
=
'../config'
;
const
Promise
=
require
(
'bluebird'
);
const
fs
=
require
(
'fs'
);
const
redis
=
require
(
'redis'
);
const
config
=
require
(
'config'
);
const
phantom
=
require
(
'phantom'
);
const
cheerio
=
require
(
'cheerio'
);
const
tough
=
require
(
'tough-cookie'
);
const
Horseman
=
require
(
'node-horseman'
);
const
moment
=
require
(
'moment'
);
var
randomInt
=
require
(
'random-int'
);
const
axios
=
require
(
'axios'
);
const
axiosCookieJarSupport
=
require
(
'@3846masa/axios-cookiejar-support'
);
// 自定义模块
const
captcha
=
require
(
'../captcha'
);
const
errors
=
require
(
'../errors'
);
Promise
.
promisifyAll
(
redis
);
Promise
.
promisifyAll
(
fs
);
axios
.
defaults
.
withCredentials
=
true
;
class
EasyPass
{
constructor
()
{
constructor
(
params
)
{
this
.
params
=
params
;
}
async
getCookies
()
{
try
{
const
client
=
await
redis
.
createClient
();
const
cookies_key
=
this
.
params
.
webname
+
'_cookies'
;
let
cookies
=
JSON
.
parse
(
await
client
.
getAsync
(
cookies_key
));
await
cookies
.
forEach
((
cookie
)
=>
{
cookie
.
expires
=
(
new
Date
()).
getTime
()
+
(
1000
*
60
*
60
);
});
await
client
.
quit
();
return
cookies
;
}
catch
(
e
)
{
return
null
;
}
}
async
execute
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
switch
(
this
.
params
.
task
)
{
case
'login'
:
{
this
.
login
();
break
;
}
case
'tryLogin'
:
{
return
this
.
tryLogin
();
}
case
'fetchClues'
:
{
this
.
fetchClues
();
break
;
}
}
});
}
async
tryLogin
()
{
try
{
let
cookies
=
await
this
.
getCookies
();
let
loginSuccess
=
false
;
if
(
cookies
)
{
console
.
log
(
JSON
.
stringify
(
cookies
));
const
horseman
=
new
Horseman
({
ignoreSSLErrors
:
true
});
loginSuccess
=
await
horseman
.
userAgent
(
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
)
.
viewport
(
1920
,
1080
)
.
cookies
(
cookies
)
.
open
(
'http://dealer.easypass.cn/HomeIndex.aspx'
)
.
screenshot
(
'try.png'
)
.
exists
(
'#myAccountLi'
);
await
horseman
.
close
();
}
if
(
loginSuccess
)
{
return
'success'
;
}
else
{
throw
new
errors
.
TryLoginError
(
'尝试登录失败'
);
}
}
catch
(
e
)
{
throw
e
;
}
}
async
login
()
{
try
{
try
{
await
this
.
tryLogin
();
console
.
log
(
'尝试登录成功'
);
return
;
}
catch
(
e
)
{
console
.
log
(
e
.
message
);
}
console
.
log
(
'开始表单登录'
);
const
username
=
this
.
params
.
username
;
const
password
=
this
.
params
.
password
;
const
horseman
=
new
Horseman
({
ignoreSSLErrors
:
true
});
await
horseman
.
userAgent
(
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
)
.
viewport
(
1920
,
1080
)
.
open
(
'http://dealer.easypass.cn/LoginPage/DefaultLogin.aspx'
)
.
screenshot
(
'm1.png'
);
console
.
log
(
'打开了登录页'
);
// 验证码存储到磁盘
const
imageFile
=
await
horseman
.
crop
(
'#imgVerificationCode'
,
'captcha1.png'
)
.
cropBase64
(
'#imgVerificationCode'
,
'PNG'
)
.
then
((
base64Text
)
=>
{
// const imageFile = 'tmp/' + (new Date) . getTime() + '.gif';
const
imageFile
=
'captcha2.png'
;
const
writeStream
=
fs
.
createWriteStream
(
imageFile
);
// data:image/png;base64, + base64Text
writeStream
.
write
(
base64Text
,
'base64'
);
return
imageFile
;
});
console
.
log
(
'验证码准备完毕'
);
// 验证码解码
let
captchaText
;
try
{
const
captchaType
=
config
.
get
(
'Dealer'
)[
this
.
params
.
webname
][
'captchaType'
];
captchaText
=
await
captcha
.
decode
(
captchaType
,
imageFile
);
console
.
info
(
'captchaText:'
,
captchaText
);
}
catch
(
e
)
{
console
.
log
(
e
.
toString
());
return
;
}
// 填写登录表单
await
horseman
.
type
(
'input[id="txtUserName"]'
,
username
)
.
type
(
'input[id="txtPwd"]'
,
password
)
.
type
(
'input[id="txtVerificationCode"]'
,
captchaText
)
.
screenshot
(
'm2.png'
);
// 提交登录表单
const
loginSuccess
=
await
horseman
.
click
(
'[id="btnLogin"]'
)
.
screenshot
(
'm2.5.png'
)
.
waitForNextPage
({
timeout
:
5000
})
.
screenshot
(
'm3.png'
)
.
exists
(
'#myAccountLi'
);
if
(
loginSuccess
)
{
console
.
log
(
'登录成功!'
);
const
cookies
=
await
horseman
.
cookies
().
close
();
console
.
log
(
JSON
.
stringify
(
cookies
));
const
cookie_key
=
this
.
params
.
webname
+
'_cookies'
;
const
client
=
await
redis
.
createClient
();
await
client
.
set
(
cookie_key
,
JSON
.
stringify
(
cookies
),
redis
.
print
);
await
client
.
quit
();
console
.
info
(
'cookie保存成功'
);
return
'success'
;
}
else
{
await
horseman
.
close
();
}
}
catch
(
e
)
{
throw
e
;
}
}
// 抓取线索
async
fetchClues
()
{
try
{
try
{
await
this
.
login
();
console
.
log
(
'登录成功'
);
}
catch
(
e
)
{
console
.
log
(
e
.
message
);
// 登录失败更新数据库cookie状态
return
;
}
const
cookies
=
await
this
.
getCookies
();
if
(
cookies
)
{
const
horseman
=
new
Horseman
({
ignoreSSLErrors
:
true
});
horseman
.
on
(
'onResourceRequested'
,
(
requestData
,
networkRequest
)
=>
{
console
.
info
(
'Requesting'
,
requestData
.
url
);
console
.
log
(
'networkRequest'
,
networkRequest
);
});
const
txtBeginTime
=
moment
().
subtract
(
9
,
'days'
).
format
(
'YYYY-MM-DD'
);
await
horseman
.
userAgent
(
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0'
)
.
viewport
(
1920
,
1080
)
.
cookies
(
cookies
)
.
open
(
'http://app.easypass.cn/lmsnew/OutmodedDefault.aspx'
)
.
waitForSelector
(
'#UpdatePanel1'
)
.
waitForSelector
(
'#pager1'
)
.
type
(
'input[id="txtBeginTime"]'
,
txtBeginTime
)
.
click
(
'input[id="btnSearch"]'
)
.
wait
(
1000
)
// .waitForNextPage()
.
screenshot
(
'list.png'
);
// TODO: if waitfor failed need some error check
let
clues
=
[];
const
fetchByPage
=
async
()
=>
{
const
html
=
await
horseman
.
html
(
'#UpdatePanel1'
);
const
$
=
cheerio
.
load
(
html
);
$
(
'#dataTable #grvNewCarOpportunity tr'
).
not
(
'.th'
).
each
(
function
(
i
,
elem
)
{
const
clue
=
{};
// const area, created;
// 主体信息样本:赵永亮[河南新乡],电话15249705814,订购大迈X5升级版 1.5T 手动 精英型2017款
const
ptitleStr
=
$
(
this
).
find
(
'.msg p'
).
attr
(
'title'
).
trim
()
||
''
;
const
ptitleStrArr
=
ptitleStr
.
split
(
','
)
||
[];
const
customerNameAreaStr
=
ptitleStrArr
[
0
].
trim
()
||
''
;
const
customerPhoneStr
=
ptitleStrArr
[
1
].
trim
()
||
''
;
const
customerExpectCarStr
=
ptitleStrArr
[
2
].
trim
()
||
''
;
const
customerNameAreaMatch
=
customerNameAreaStr
.
match
(
/
(
.*
?)\[(
.*
?)\]
/
);
clue
.
customerName
=
customerNameAreaMatch
[
1
].
trim
()
||
''
;
clue
.
customerArea
=
customerNameAreaMatch
[
2
].
trim
()
||
''
;
clue
.
customerPhone
=
customerPhoneStr
.
replace
(
'电话'
,
''
).
trim
();
clue
.
customerExpectCar
=
customerExpectCarStr
.
replace
(
'订购'
,
''
).
trim
();
// 提交时间样本:提交时间:2017-08-10 08:31
const
customerCreatedTimeStr
=
$
(
this
).
find
(
'.msg p a span:contains(提交时间)'
).
text
();
clue
.
customerCreatedTime
=
customerCreatedTimeStr
.
replace
(
'提交时间:'
,
''
).
trim
();
clue
.
SalesName
=
$
(
this
).
find
(
'td span[title]'
).
text
().
trim
();
clues
.
push
(
clue
);
console
.
log
(
'客户序号:'
,
clues
.
length
);
console
.
log
(
'客户姓名:'
,
clue
.
customerName
);
console
.
log
(
'上牌地区:'
,
clue
.
customerArea
);
console
.
log
(
'客户号码:'
,
clue
.
customerPhone
);
console
.
log
(
'负责销售:'
,
clue
.
SalesName
);
console
.
log
(
'线索获得时间:'
,
clue
.
customerCreatedTime
);
console
.
log
(
'线索意向车型:'
,
clue
.
customerExpectCar
);
console
.
log
(
'==========================================================================='
);
});
// 判断是否最后一页有两种情况:1.只有一页时分页按钮不存在 2.末页按钮置灰
const
isLastPage
=
!
$
(
'#pager1'
).
length
||
$
(
'#pager1 a:contains(末页)'
).
prop
(
'disabled'
);
if
(
!
isLastPage
)
{
await
horseman
.
click
(
'a.paginator:contains(下一页)'
)
.
screenshot
(
'list2.png'
)
// .waitForNextPage()
.
wait
(
1000
)
.
waitForSelector
(
'#UpdatePanel1'
)
.
screenshot
(
'list3.png'
);
let
randomSecs
=
randomInt
(
1
,
5
);
console
.
log
(
`随机等待
${
randomSecs
}
秒`
);
await
Promise
.
delay
(
randomSecs
*
1000
).
then
(
async
function
()
{
await
fetchByPage
();
});
}
}
await
fetchByPage
();
await
horseman
.
close
();
return
}
else
{
throw
new
Error
(
'no cookie'
);
}
let
rowcount
;
let
customerIndex
=
0
;
const
fetchByPage
=
async
()
=>
{
if
(
!
rowcount
)
{
console
.
log
(
`线索日期:
${
params
.
startTime
}
-
${
params
.
endtime
}
`
);
}
console
.
log
(
`线索页面:
${
params
.
pageIndex
}
`
);
axios
.
get
(
'http://ics.autohome.com.cn/Dms/Order/SearchOrderList?'
,
{
params
:
params
,
jar
:
cookieJar
,
withCredentials
:
true
}).
then
(
async
res
=>
{
const
clues
=
res
.
data
;
if
(
!
rowcount
)
{
rowcount
=
clues
.
result
.
rowcount
;
console
.
log
(
'只进入一次总数:'
,
rowcount
);
}
for
(
let
clue
of
clues
.
result
.
list
)
{
customerIndex
++
;
let
$
=
cheerio
.
load
(
clue
.
CustomerNameStr
);
let
idMatch
=
$
.
html
().
match
(
/showCusDetailWithId
\(
"
(\d
*
)
"
\)
/
);
clue
.
CustomerId
=
idMatch
[
1
]
||
0
;
console
.
log
(
'客户序号:'
,
customerIndex
);
console
.
log
(
'客户ID:'
,
clue
.
CustomerId
);
console
.
log
(
'客户姓名:'
,
$
.
text
());
console
.
log
(
'客户号码:'
,
clue
.
CustomerPhoneStr
);
console
.
log
(
'负责销售:'
,
clue
.
SalesNameStr
);
console
.
log
(
'线索类型:'
,
clue
.
OrderTypeStr
);
console
.
log
(
'线索状态:'
,
clue
.
OrderStateStr
);
console
.
log
(
'线索获得时间:'
,
clue
.
LeadTimeStr
);
console
.
log
(
'上牌地区:'
,
clue
.
LicenseCityStr
);
console
.
log
(
'线索意向车型:'
,
clue
.
SpecNameStr
);
console
.
log
(
'==========================================================================='
);
}
if
(
customerIndex
<
rowcount
)
{
let
randomSecs
=
randomInt
(
1
,
8
);
console
.
log
(
`随机等待
${
randomSecs
}
秒`
);
await
Promise
.
delay
(
randomSecs
*
1000
).
then
(
async
function
()
{
params
.
pageIndex
++
;
await
fetchByPage
();
});
}
});
}
fetchByPage
(
params
.
pageindex
);
}
catch
(
e
)
{
console
.
dir
(
e
.
stack
);
// throw e;
}
}
}
...
...
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