Java爬虫_资源网站爬取实战

public static String getBookUrlCode(String url) throws IOException {
2 URL u ;
3 HttpURLConnection httpURLConnection ;
4 String ret = “” ;
5 try{
6 u = new URL(url);
7 httpURLConnection = (HttpURLConnection)u.openConnection() ;
8 if(httpURLConnection.getResponseCode()==200){
9 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),“utf-8”)) ;
10
11 String read ;
12 while((read=bufferedReader.readLine())!=null){
13 ret += read ;
14 ret+="\r\n" ;
15 }
16 }
17 }catch (Exception e){
18
19 }
20 return ret ;
21 }
复制代码

运行结果:

获取主页的源码之后,通过解析获得书籍分类的地址,用一个ArrayList保存 。

解析方法 : 正则表达式,jsoup

复制代码
1 public static ArrayList getBookClass(String read){
2 ArrayList arrayList = new ArrayList() ;
3 String data = new String( "<a href=“http:(.*)html>”) ;
4 Document doc = Jsoup.parse(read);
5 Elements elements = doc.select(“a”);
6 for(Element element : elements){
7 String aurl = element.attr(“href”) ;
8 if(!arrayList.contains(aurl)){
9 arrayList.add(aurl);
10 }
11 }
12 return arrayList ;
13 }
复制代码
    运行结果:

复制代码
1 public static void main(String[] args) throws Exception {
2 ArrayList arrayList= getBookClass(getBookUrlCode(“http://.com/”));
3 for(int i=0;i<arrayList.size();i++){
4 System.out.println(arrayList.get(i));
5 }
6 }
复制代码

需要的书籍分类的链接可以看到是 bestcbooks.com 加 /categories/(书籍种类)/

所以在获取的ArrayList中需要的只是 /categories/(书籍种类)/ 这一部分。

可以采用正则表达式解析筛选

只需要在之前代码ArrayList . add() 操作之前,进行正则表达式筛选即可

修改代码如下:

复制代码
1 public static ArrayList getBookClass(String read){
2 ArrayList arrayList = new ArrayList() ;
3 String data = new String( "<a href=“http:(.)html>") ;
4 Document doc = Jsoup.parse(read);
5 Elements elements = doc.select(“a”);
6 for(Element element : elements){
7 String aurl = element.attr(“href”) ;
8 String con = "/categories(.
)” ;
9 Pattern ah = Pattern.compile(con);
10 Matcher mr = ah.matcher(aurl);
11 while(mr.find()){
12 if(!arrayList.contains(mr.group())){
13 arrayList.add(mr.group());
14 }
15 }
16 }
17 return arrayList ;
18 }
复制代码
运行结果:

要进入上面书籍种类对应网页的链接已经拿到了,也就是 ArrayList中对应的值与 http://bestcbooks.com 加 ArrayList.get(i)

现在拿到了这些类型书籍的链接,可以继续通过之前的getBookUrlCode(String url)来获取对应源码,这样就可以拿到某种类型的书籍地址了。

书籍分享对应的链接需要在源码中找,结果分析,是 中的这段字符,还有一些链接是关于买书的,不是分享页。

它们的特点是每个后面都有一个下载图标,也就是<img src="/images/download.png" ,可以利用这一点很好的讲关键字符利用正则表达式切出 。

关于种类获取书籍的方法代码如下,返回是一个ArrayList

复制代码
1 public static ArrayList getBook(String read){
2 ArrayList arrayList = new ArrayList() ;
3
4 String con = “<a href=(.*)<img src=”/images/download" ;
5 Pattern ah = Pattern.compile(con);
6 Matcher mr = ah.matcher(read);
7 System.out.println(“find”);
8 while(mr.find()) {
9 if (!arrayList.contains(mr.group())) {
10 arrayList.add(mr.group());
11 }
12 }
13 return arrayList ;
14 }
复制代码
返回ArrayList

书籍对应的链接也已经可以找到了,接下来就是进入这个链接,拿到百度网盘的地址(有些会有密码,也要一起拿到)

获得网盘链接和密码的方法:

复制代码
1 public static void find(String read){
2 String con = “<a href=”(.)pan.baidu.com(.)ref" ;
3 Pattern ah = Pattern.compile(con);
4 Matcher mr = ah.matcher(read);
5 while(mr.find()) {
6 String []bookPan = mr.group().split(""") ;
7 String bookM = bookPan[1] ;
8 System.out.print(bookM+" ");
9
10 }
11 }
12 public static void getM(String read){
13 String con = “密码(.*)” ;
14 Pattern ah = Pattern.compile(con);
15 Matcher mr = ah.matcher(read);
16 while(mr.find()) {
17 System.out.print(mr.group());
18
19 }
20 System.out.println();
21 }
复制代码
测试结果:

还可以通过正则将书籍名称,评分等信息找出。

以下是完整代码:

复制代码
1 package Book;
2
3 import org.jsoup.Jsoup;
4 import org.jsoup.nodes.Document;
5 import org.jsoup.nodes.Element;
6 import org.jsoup.select.Elements;
7
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.net.HttpURLConnection;
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.Queue;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 public class GetBook {
19
20 public static String getBookUrlCode(String url) throws IOException {
21 URL u ;
22 HttpURLConnection httpURLConnection ;
23 String ret = “” ;
24 try{
25 u = new URL(url);
26 httpURLConnection = (HttpURLConnection)u.openConnection() ;
27 if(httpURLConnection.getResponseCode()==200){
28 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),“utf-8”)) ;
29
30 String read ;
31 while((read=bufferedReader.readLine())!=null){
32 ret += read ;
33 ret+="\r\n" ;
34 }
35 }
36 }catch (Exception e){
37
38 }
39 return ret ;
40 }
41
42
43 public static ArrayList getBookClass(String read){
44 ArrayList arrayList = new ArrayList() ;
45 Document doc = Jsoup.parse(read);
46 Elements elements = doc.select(“a”);
47 for(Element element : elements){
48 String aurl = element.attr(“href”) ;
49 String con = “/categories(.)" ;
50 Pattern ah = Pattern.compile(con);
51 Matcher mr = ah.matcher(aurl);
52 while(mr.find()){
53 if(!arrayList.contains(mr.group())){
54 arrayList.add(mr.group());
55 }
56 }
57 }
58 return arrayList ;
59 }
60
61 public static ArrayList getBook(String read){
62 ArrayList arrayList = new ArrayList() ;
63
64 String con = "<a href=(.
)<img src=”/images/download" ;
65 Pattern ah = Pattern.compile(con);
66 Matcher mr = ah.matcher(read);
67 while(mr.find()) {
68 if (!arrayList.contains(mr.group())) {
69 arrayList.add(mr.group());
70 }
71 }
72 return arrayList ;
73 }
74 public static void find(String read){
75 String con = “<a href=”(.)pan.baidu.com(.)ref" ;
76 Pattern ah = Pattern.compile(con);
77 Matcher mr = ah.matcher(read);
78 while(mr.find()) {
79 String []bookPan = mr.group().split(""") ;
80 String bookM = bookPan[1] ;
81 System.out.print(bookM+" “);
82
83 }
84 }
85 public static void getM(String read){
86 String con = “密码(.*)” ;
87 Pattern ah = Pattern.compile(con);
88 Matcher mr = ah.matcher(read);
89 while(mr.find()) {
90 System.out.print(mr.group());
91
92 }
93 System.out.println();
94 }
95 public static void main(String[] args) throws Exception {
96 ArrayList arrayList= getBookClass(getBookUrlCode(“http://bestcbooks.com/”));
97 for(int i=0;i<arrayList.size();i++){
98 String read = getBookUrlCode(“http://bestcbooks.com”+arrayList.get(i));
99 ArrayList book = getBook(read);
100 for(int j=0;j<book.size();j++){
101 String[] bookIn = book.get(j).split(”"");
102 String myBook = bookIn[1] ;
103 String myBookCode = getBookUrlCode(“http://bestcbooks.com”+myBook);
104 //System.out.println(myBookCode);
105 find(myBookCode);
106 getM(myBookCode);
107 }
108 }
109 }
110 }
www.yisuping.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值