最近小伙伴让我帮他下载某度文库的文档,所以打算写个爬虫。
在网上查找了一下,发现有位大佬用python写了一个程序
由于python我只自学过几天,很多操作不是很懂,安装docx包失败所以打算改成Java语言。
参考文章:https://blog.csdn.net/foolcuntry/article/details/105478385
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author chaos
* @date 2020/5/21
* 文库爬虫,VIP文档除外
*/
public class UrlTest {
@Test
public void tets1() throws IOException {
String path = "https://wenku.baidu.com/view/7deba6a8fd4ffe4733687e21af45b307e971f966";
String data = "";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
if (conn.getResponseCode() == 200) {
// 请求返回的数据
InputStream in = conn.getInputStream();
data = IOUtils.toString(in, "GBK");
System.out.println("连接成功");
in.close();
replace(data);
} else {
System.out.println(conn.getResponseCode() + ":no++");
}
}
public void replace(String data) throws IOException {
Pattern pat = Pattern.compile("WkInfo.htmlUrls = '(.*)'");
Matcher matcher = pat.matcher(data);
String res="";
if (matcher.find()){
res=matcher.group(1);
}
res = res.replace("\\x22", "\"");
res=res.replaceAll("\\\\","");
JSONObject jsonObject = JSONObject.parseObject(res);
JSONArray jsonArray = jsonObject.getJSONArray("json");
String txtx="";
for (int i=0;i<jsonArray.size();i++){
JSONObject object = jsonArray.getJSONObject(i);
String pageLoadUrl = object.getString("pageLoadUrl");
URL url = new URL(pageLoadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
//这里返回的状态码是206
InputStream in = conn.getInputStream();
String value = IOUtils.toString(in, "GBK");
in.close();
Pattern pattern = Pattern.compile("wenku_\\d*\\((.*)\\)");
Matcher mat = pattern.matcher(value);
String doc="";
if (mat.find()){
doc=mat.group(1);
}
JSONObject jsonObject1 = JSONObject.parseObject(doc);
JSONArray body = jsonObject1.getJSONArray("body");
for (int j=0;j<body.size();j++){
JSONObject text = body.getJSONObject(j);
if ("word".equals(text.getString("t"))){
txtx +=text.getString("c");
JSONObject ps = text.getJSONObject("ps");
if(ps!=null && "1".equals(ps.getString("_enter"))){
txtx +="\n";
}
}
}
}
//控制台输出
//System.out.println(txtx);
//写到文件中
FileWriter fw = new FileWriter("F:\\a.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txtx);
bw.close();
fw.close();
}
}
文章中需要用到阿里巴巴的fastjson包,解析json数据。
插播一条小技巧,需要找jar包的话可以直接搜索 maven 包名,
如:maven fastjson,就可以在maven仓库下载相应的jar包了。
最终文档是以TXT格式把保存,格式和图片并没有爬取。
第一次爬取数据格式如下:
PNG图片数据没有爬,感兴趣的同学可以试一下。
根据第一次爬取得到的url爬下来的数据如下:
最终数据如下:
最终代码没有去优化,如果有疑问或者有什么好的建议的欢迎评论留言。