TamperMonkey脚本以及JavaScript爬虫应用

本文最后更新于:2024年3月20日 中午

前言

在以往的XmlHttprequest对象中想要跨域请求基本上就是靠jsonp,油猴脚本可以实现自定义网页脚本,但是他同样无法避免的要被CORS阻止。

如何使用 @grant注解 实现使用油猴自带的GM_xmlhttpRequest发送跨域请求?

通常新建一个脚本之后是这样的:
符号 // 不是代表注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match url
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';

// Your code here...

@grant可以使用一些加强函数这些函数都是以GM_开头的。
如果@grant是none的话就只能使用GM_info这个函数了。

之后我们的代码中就可以使用GM_xmlhttpRequest函数了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.uuuuuu.com/mdeditor
// @grant GM_xmlhttpRequest
// @grant GM_download
// ==/UserScript==
/* jshint -W097 */
'use strict';
GM_xmlhttpRequest({
method: "GET",
url: "http://www.qq.com/",
onload: function(response) {
//这里写处理函数
}
});

处理这些属性之外还有一些其他的属性:

url - the URL from where the data should be downloaded
name - thefilename - for security reasons the file extension needs to bewhitelisted at the Tampermonkey options page
headers - seeGM_xmlhttpRequest for more details saveAs - boolean value, show asaveAs dialog
onload - function() {} - callback function that iscalled when the download has finished
onerror - function(download) {}
callback function that is called when there was an error

下面给出一个通过js爬取某个网站信息的例子:

jquery 中 ('div','li')表示的是(子,父),即从父节点里寻找子节点,是选择li标签里面所有div标签,而不是找li标签外面的div标签。
比如:

1
2
3
4
5
var el = $( '<div></div>' );
el.html(data);
var tda= $("div.content div.header a.name em", el) ;
var detailUrl = $("div.content div.header a.name ", el) ;

完整例子,只是demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ==UserScript==
// @name 爬取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 爬取
// @author bob
// @match https://www.txxxxxx.com/search*
// @requir https://code.jquery.com/jquery-latest.js
// @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
// @run-at document-idle
// @grant GM_xmlhttpRequest
// ==/UserScript==

(function () {
'use strict';
var $ = $ || window.$;
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
var enterpriseList = [ ];
var dataList = [];
var errorListIds = [];
//获取企业列表
GM_xmlhttpRequest({
method: 'GET',
url: "http://xxx.xxxxxx.com/yyyyyy/queryEnterpriseList?startID=44633&endID=45000",
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
enterpriseList = $.parseJSON( response.responseText );
getData();
},
});

//saveData(data);
var count = 0;
function saveData(data){
GM_xmlhttpRequest({
method: 'POST',
data:JSON.stringify(data),
url: "http://xxx.xxxxx.com/yyyyyy/saveDataList",
dataType: "json",
headers: {
"Content-Type": "application/json"
},
onload: function(response) {

},
});
}


function getData(){
for(var i=0;i<enterpriseList.length;i++){//
var one = {};
var url = 'https://www.uuuuuuuuu.com/search?key='+enterpriseList[i].enterpriseName.replace("(","(").replace(")",')');
try{
var num=Math.floor(Math.random()*10+5);//随机等待几秒
sleep(num*1000);
$.ajax({url:url,
async:false,
// timeout:5000,
success:function(data){
var str = "index_verify?type=companysearch"
if(data.indexOf(str) != -1){
i = enterpriseList.length;
console.log('****************************************操作需要验证');
}

var el = $( '<div></div>' );
el.html(data);
var tda= $("div.content div.header a.name em", el) ;
var detailUrl = $("div.content div.header a.name ", el) ;

if(tda.length >0 && tda[0].innerText == enterpriseList[i].enterpriseName){
$.ajax({url:detailUrl[0].href,
async:false,
// timeout:5000,
success:function(data){
var el = $( '<div></div>' );
el.html(data);

one.enterpriseName = enterpriseList[i].enterpriseName ;//公司名称

var tr = $("#_container_baseInfo table.table.-striped-col.-border-top-none tr", el);
if(businessTerm.length>8){
if(businessTerm.substring(0,3) != '***'){
one.startTime = businessTerm.substring(0,9) ;
if(businessTerm.substr(11) == '无固定期限'){
one.endTime = '2099-12-31';
}else{
one.endTime = businessTerm.substr(11);
}
}else{
one.startTime = td[12].innerText.replace(/\s+/g,"").replace(/[\n\r]/g,'') ;
if(businessTerm.substr(4) == '无固定期限'){
one.endTime = '2099-12-31';
}else{
one.endTime = businessTerm.substr(4);
}
}
}
}, error:function(data){}});
}else{
one.qualificationNo = '--';
}
dataList.push(one);
if(dataList.length == 10){
saveData(dataList);//保存数据
dataList = [];
count += 10;
console.log('已保存数据:'+count);
}
}, error:function(data){ }});
}catch(e){
}
}
}
})();