Java爬虫实战(二):抓取一个视频网站上2015年所有电影的下载链接
来源:互联网 发布:手机经纬度查询软件 编辑:程序博客网 时间:2024/06/05 18:38
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package
action;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
java.util.LinkedHashMap;
import
java.util.Map;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
VideoLinkGrab {
public
static
void
main(String[] args) {
VideoLinkGrab videoLinkGrab =
new
VideoLinkGrab();
videoLinkGrab.saveData(
"http://www.80s.la/movie/list/-2015----p"
);
}
/**
* 将获取到的数据保存在数据库中
*
* @param baseUrl
* 爬虫起点
* @return null
* */
public
void
saveData(String baseUrl) {
Map<String, Boolean> oldMap =
new
LinkedHashMap<String, Boolean>();
// 存储链接-是否被遍历
Map<String, String> videoLinkMap =
new
LinkedHashMap<String, String>();
// 视频下载链接
String oldLinkHost =
""
;
// host
Pattern p = Pattern.compile(
"(https?://)?[^/\\s]*"
); // 比如:http://www.zifangsky.cn
Matcher m = p.matcher(baseUrl);
if
(m.find()) {
oldLinkHost = m.group();
}
oldMap.put(baseUrl,
false
);
videoLinkMap = crawlLinks(oldLinkHost, oldMap);
// 遍历,然后将数据保存在数据库中
try
{
Connection connection = JDBCDemo.getConnection();
for
(Map.Entry<String, String> mapping : videoLinkMap.entrySet()) {
PreparedStatement pStatement = connection
.prepareStatement(
"insert into movie(MovieName,MovieLink) values(?,?)"
);
pStatement.setString(
1
, mapping.getKey());
pStatement.setString(
2
, mapping.getValue());
pStatement.executeUpdate();
pStatement.close();
// System.out.println(mapping.getKey() + " : " + mapping.getValue());
}
connection.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
/**
* 抓取一个网站所有可以抓取的网页链接,在思路上使用了广度优先算法 对未遍历过的新链接不断发起GET请求, 一直到遍历完整个集合都没能发现新的链接
* 则表示不能发现新的链接了,任务结束
*
* 对一个链接发起请求时,对该网页用正则查找我们所需要的视频链接,找到后存入集合videoLinkMap
*
* @param oldLinkHost
* 域名,如:http://www.zifangsky.cn
* @param oldMap
* 待遍历的链接集合
*
* @return 返回所有抓取到的视频下载链接集合
* */
private
Map<String, String> crawlLinks(String oldLinkHost,
Map<String, Boolean> oldMap) {
Map<String, Boolean> newMap =
new
LinkedHashMap<String, Boolean>();
// 每次循环获取到的新链接
Map<String, String> videoLinkMap =
new
LinkedHashMap<String, String>();
// 视频下载链接
String oldLink =
""
;
for
(Map.Entry<String, Boolean> mapping : oldMap.entrySet()) {
// System.out.println("link:" + mapping.getKey() + "--------check:"
// + mapping.getValue());
// 如果没有被遍历过
if
(!mapping.getValue()) {
oldLink = mapping.getKey();
// 发起GET请求
try
{
URL url =
new
URL(oldLink);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod(
"GET"
);
connection.setConnectTimeout(
2500
);
connection.setReadTimeout(
2500
);
if
(connection.getResponseCode() ==
200
) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(inputStream,
"UTF-8"
));
String line =
""
;
Pattern pattern =
null
;
Matcher matcher =
null
;
//电影详情页面,取出其中的视频下载链接,不继续深入抓取其他页面
if
(isMoviePage(oldLink)){
boolean
checkTitle =
false
;
String title =
""
;
while
((line = reader.readLine()) !=
null
) {
//取出页面中的视频标题
if
(!checkTitle){
pattern = Pattern.compile(
"([^\\s]+).*?</title>"
);
matcher = pattern.matcher(line);
if
(matcher.find()){
title = matcher.group(
1
);
checkTitle =
true
;
continue
;
}
}
// 取出页面中的视频下载链接
pattern = Pattern
.compile(
"(thunder:[^\"]+).*thunder[rR]es[tT]itle=\"[^\"]*\""
);
matcher = pattern.matcher(line);
if
(matcher.find()) {
videoLinkMap.put(title,matcher.group(
1
));
System.out.println(
"视频名称: "
+ title +
" ------ 视频链接:"
+ matcher.group(
1
));
break
;
//当前页面已经检测完毕
}
}
}
//电影列表页面
else
if
(checkUrl(oldLink)){
while
((line = reader.readLine()) !=
null
) {
pattern = Pattern
.compile(
"<a href=\"([^\"\\s]*)\""
);
matcher = pattern.matcher(line);
while
(matcher.find()) {
String newLink = matcher.group(
1
).trim();
// 链接
// 判断获取到的链接是否以http开头
if
(!newLink.startsWith(
"http"
)) {
if
(newLink.startsWith(
"/"
))
newLink = oldLinkHost + newLink;
else
newLink = oldLinkHost +
"/"
+ newLink;
}
// 去除链接末尾的 /
if
(newLink.endsWith(
"/"
))
newLink = newLink.substring(
0
,
newLink.length() -
1
);
// 去重,并且丢弃其他网站的链接
if
(!oldMap.containsKey(newLink)
&& !newMap.containsKey(newLink)
&& (checkUrl(newLink) || isMoviePage(newLink))) {
System.out.println(
"temp: "
+ newLink);
newMap.put(newLink,
false
);
}
}
}
}
reader.close();
inputStream.close();
}
connection.disconnect();
}
catch
(MalformedURLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
oldMap.replace(oldLink,
false
,
true
);
}
}
// 有新链接,继续遍历
if
(!newMap.isEmpty()) {
oldMap.putAll(newMap);
videoLinkMap.putAll(crawlLinks(oldLinkHost, oldMap));
// 由于Map的特性,不会导致出现重复的键值对
}
return
videoLinkMap;
}
/**
* 判断是否是2015年的电影列表页面
* @param url 待检查URL
* @return 状态
* */
public
boolean
checkUrl(String url){
Pattern pattern = Pattern.compile(
"http://www.80s.la/movie/list/-2015----p\\d*"
);
Matcher matcher = pattern.matcher(url);
if
(matcher.find())
return
true
;
//2015年的列表
else
return
false
;
}
/**
* 判断页面是否是电影详情页面
* @param url 页面链接
* @return 状态
* */
public
boolean
isMoviePage(String url){
Pattern pattern = Pattern.compile(
"http://www.80s.la/movie/\\d+"
);
Matcher matcher = pattern.matcher(url);
if
(matcher.find())
return
true
;
//电影页面
else
return
false
;
}
}
2 0
- Java爬虫实战(二):抓取一个视频网站上2015年所有电影的下载链接
- Java爬虫实战(一):抓取一个网站上的全部链接
- Java爬虫爬取网站电影下载链接
- Python爬虫实战(八):爬取电影天堂的电影下载链接
- 基于python实现的抓取腾讯视频所有电影的爬虫
- python 爬虫(二)一个带下载进度的网页抓取小爬虫
- 使用java 爬虫 抓取youtube,youku,facebook 等视频网站的视频数据(请求规则的分析)
- 用python实现一个抓取电影的爬虫
- Scrapy框架抓取豆瓣电影的小爬虫学习日记(二)
- 爬虫学习(一)---爬取电影天堂下载链接
- Python多线程爬虫获取电影下载链接
- java爬虫抓取网络上的图片
- java jsoup 网络爬虫 学习例子(二) 只抓取豆瓣电影5星(力荐)电影名称
- 四脚猫php视频下载 检测一个网页上的链接是否都可访问(练一练)
- Python实现抓取页面上链接的简单爬虫分享
- Python实现抓取页面上链接的简单爬虫分
- 【Python爬虫】批量抓取网页上的视频
- 下载电影的网站
- 通过索引或指针对数组元素进行访问
- 以梦喂马,驰骋岁月;以梦为马,诗酒趁年华。
- 珍重
- JNI简单实现demo
- Android Gradle文件下获取自定义properties文件,并且获取值或者修改值
- Java爬虫实战(二):抓取一个视频网站上2015年所有电影的下载链接
- Java爬虫实战(一):抓取一个网站上的全部链接
- camera应用
- Apache2.4+Tomcat7服务器集群配置
- 刷清橙OJ--A1009.数列特征2
- 三大主流前端框架与项目开发
- unable to load default svn client 和 Eclipse SVN 插件与TortoiseSVN对应关系
- hdu 4609 3-idiots
- 闭包的理解