本帖最后由 ~零度 于 2018-9-24 13:45 编辑
最近在某影视资源网站上下载动漫,大约有30多集,手动的话需要一条条地复制每一集的磁力链接然后下载,比较麻烦,于是写了这个提取磁力链接的小工具。
理论上任何支持html代码内包含磁力链接的网页,但是可能因为某些网站特殊的反爬虫措施或者其他的保护措施等导致获取不了。
因为磁力链接可能因为资源数太少导致下载速度很慢,所以下一步打算利用百度云的api将提取出来的磁力链接添加到百度云离线下载里,然后利用Pandownload等工具下载。
目前可以用本程序先提取出磁力链接,在利用百度云批量离线脚本(https://greasyfork.org/zh-CN/scripts/23426-%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%E6%89%B9%E9%87%8F%E7%A6%BB%E7%BA%BF)批量建立离线下载,但是太多的话貌似会报错,需要等一段时间。
本人不常用java且一般不做UI,所以技术较弱,请各位大佬见谅,如有问题和建议,欢迎讨论!
网页内磁力链接
软件界面如下图:
获取磁力链接
百度云批量离线脚本:
在URL的框内输入包含磁力链接的网址,然后点击OK,稍等一会就会在下面的框里输出所有的磁力链接。有时候会因为网络等原因,导致无法获取到所有的网页代码,就会显示“未获取到所有内容”,这时等一会再重试应该就好了。然后复制下面提取出来的磁力链接,打开迅雷(如果遇到版权问题,论坛上有破解版的迅雷)就可以下载了。
下载链接:链接:https://pan.baidu.com/s/1lYzZlx9wlVi09sOh071eUg 密码:plaf
请注意:如果你的电脑上已经安装了JAVA环境,只需下载里面的jar包,如果没有则需要下载rar压缩包解压后运行里面的exe文件。
源代码:
[Java] 纯文本查看 复制代码 package getMagnet;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
final class ReadHtml extends Thread{
public String pageUrl, encoding;
public StringBuffer[] html;
ReadHtml(String _pageUrl, String _encoding, StringBuffer[] _html) {
pageUrl = _pageUrl;
encoding = _encoding;
html = _html;
}
@Override
public void run() {
try {
URL url = new URL(pageUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(false);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36");
conn.connect();
int code = conn.getResponseCode();
if(code!=HttpURLConnection.HTTP_OK){
throw new Exception("HttpURLConnection Error! Response Code = "+code);
}
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), encoding);
BufferedReader in = new BufferedReader(isr);
String line;
while((line = in.readLine())!=null) {
html[0].append(line);
}
in.close();
conn.disconnect();
}catch(Exception e) {
System.err.println(e);
}
}
}
public class GetMagnet {
private JFrame frame;
private JTextField URL;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GetMagnet window = new GetMagnet();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GetMagnet() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setTitle("\u83B7\u53D6\u78C1\u529B\u94FE\u63A5");
frame.setBounds(100, 100, 672, 502);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("URL");
lblNewLabel.setBounds(10, 23, 54, 15);
frame.getContentPane().add(lblNewLabel);
URL = new JTextField();
URL.setBounds(63, 20, 511, 21);
frame.getContentPane().add(URL);
URL.setColumns(1000);
JTextArea Magnet = new JTextArea();
Magnet.setBounds(63, 58, 583, 396);
JScrollPane scrollMagnet = new JScrollPane(Magnet);
scrollMagnet.setBounds(63, 58, 583, 396);
scrollMagnet.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollMagnet.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(scrollMagnet);
JButton OK = new JButton("OK");
OK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
StringBuffer html[] = {new StringBuffer()};
ReadHtml rh = new ReadHtml(URL.getText(), "UTF-8", html);
boolean success = false;
try {
rh.start();
for(int i=5;i>0;--i) {
frame.setTitle("\u83B7\u53D6\u78C1\u529B\u94FE\u63A5\uFF08\u7B49\u5F85"+i+"\u79D2...\uFF09");
rh.join(1000);
}
if(rh.isAlive()) {
rh.stop();
success = false;
}else {
success = true;
}
} catch (Exception ex) {
System.err.println(ex);
}
String html0 = html[0].toString();
int index = 0;
StringBuffer magnets = new StringBuffer();
int count = 0;
String regex = "magnet:\\?xt=urn:btih:[A-za-z0-9]{40,40}";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(html0);
LinkedList<String> MagnetList = new LinkedList();
while(matcher.find()) {
String temp = matcher.group();
boolean exist = false;
for(String Magnet:MagnetList) {
if(Magnet.equals(temp)) {
exist = true;
break;
}
}
if(!exist) {
magnets.append(temp+"\n");
MagnetList.add(temp);
++count;
}
}
if(success) {
frame.setTitle("\u83B7\u53D6\u78C1\u529B\u94FE\u63A5\uFF08\u5DF2\u83B7\u53D6"+count+"\u6761\uFF09");
}else {
frame.setTitle("\u83B7\u53D6\u78C1\u529B\u94FE\u63A5 \u672A\u83B7\u53D6\u6240\u6709\u5185\u5BB9\uFF0C\u8bF7\u91CD\u8BD5\uFF01\uFF08\u5DF2\u83B7\u53D6"+count+"\u6761\uFF09");
}
Magnet.setText(magnets.toString());
}
});
OK.setBounds(584, 19, 62, 23);
frame.getContentPane().add(OK);
JLabel lblNewLabel_1 = new JLabel("Magnet");
lblNewLabel_1.setBounds(10, 62, 54, 15);
frame.getContentPane().add(lblNewLabel_1);
}
}
|