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
| package spider;
import org.javaweb.core.net.HttpRequest; import org.javaweb.core.net.HttpResponse; import utils.MysqlConnection; import java.net.URL; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class DomainsSpider {
public static void main(String[] args) { Map<String[], Boolean> urls = new HashMap<String[], Boolean>(); urls.put(new String[]{"http://jeary.org", "Jeary"}, false); crawllLinks(urls); }
public static void crawllLinks(Map<String[], Boolean> urlMap) { Map<String[], Boolean> urls = new HashMap<String[], Boolean>(); String urlString = ""; String name = ""; for (Map.Entry<String[], Boolean> url : urlMap.entrySet()) { urlString = url.getKey()[0]; name = url.getKey()[1]; System.out.println(urlString+"\t"+name); int siteid = findID(urlString); if(siteid == 0){ add(new Object[]{name,urlString}); siteid = findID(urlString); } System.out.println("新的父节点ID:"+siteid+"\t"); String body = getHtml(urlString); if (body != null && !body.isEmpty()) { Map<String, String> links = getLinks(body, urlString); for (Map.Entry<String, String> linkMap : links.entrySet()) { String link = linkMap.getKey(); String title = linkMap.getValue(); urls.put(new String[]{link,title},false); System.out.println("\t"+link+"\t"+title); int ref_id = findID(link); if(ref_id == 0){ add(new Object[]{title,link}); ref_id = findID(link); } System.out.println("发现新关系,准备入库:"+siteid+" -- "+ref_id); add_ref(new Object[]{siteid,ref_id}); } System.out.println(""); } } crawllLinks(urls); }
public static String getHtml(String url) { String body = ""; try { HttpRequest request = new HttpRequest(); request.url(url); request.followRedirects(true); request.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/47.0.2526.73 Safari/537.36"); HttpResponse response = request.get(); body = response.body(); } catch (Exception e) { e.printStackTrace(); } return body; }
public static Map<String, String> getLinks(String body, String excuDomain) { Map<String, String> links = new HashMap<String, String>(); String regexp = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>"; try { if (body != null && !body.isEmpty()) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(body);
while (matcher.find()) { String link = matcher.group(2); String title = matcher.group(5);
if (title.matches("<.*>.*")) { title = title.replaceAll("<.*?>", ""); } if (link != null) { if (link.indexOf(excuDomain) < 0 && link.startsWith("http")) { URL url = new URL(link); link = url.getProtocol() + "://" + url.getHost(); boolean isBlack = excuDomainList(link); if (isBlack == false) { links.put(link, title); } } } } } } catch (Exception e) { System.out.println("异常匹配:" + excuDomain + "\r\n" + body); } return links; }
public static void add(Object[] domains) { try { String sql = "insert into sites(name,domain) value (?,?)"; MysqlConnection mysqlConnection = new MysqlConnection(); mysqlConnection.execUpdate(sql, domains); } catch (Exception e) { System.out.println("插入链接数据异常,异常信息:"+e.getLocalizedMessage()); } }
public static void add_ref(Object[] rel) { try { String sql = "insert into rel_sites(site_id,rel_site_id) value (?,?)"; MysqlConnection mysqlConnection = new MysqlConnection(); mysqlConnection.execUpdate(sql,rel); } catch (Exception e) { System.out.println("插入关系数据异常,异常信息:"+e.getLocalizedMessage()); } }
public static int findID(String domain) { int id = 0; String sql = "select id from sites where domain = ?;"; if(domain.startsWith("http://www.")){ domain = domain.replaceAll("http\\:\\/\\/www\\.","http://"); sql = "select id from sites where domain like ?"; } MysqlConnection mysqlConnection = new MysqlConnection(); List<Object> list = mysqlConnection.execGetList(sql, new Object[]{domain}); if (list != null && list.size() > 0) { id = Integer.parseInt(list.get(0).toString()); } return id; }
public static boolean excuDomainList(String domain) { String[] black = {".gov.cn", "github.com", ".apple.com", "weibo.com", ".google.com", ".csdn.net", "" + ".admin5.com", ".51.la", ".docker.com", "127.0.0.1", ".emlog.net", ".facebook.com", ".qq.com", "" + ".dockerone.com", ".mongodb.com", ".kindsoft.net", "twitter.com", ".gitlab.com", ".twitter.com", "youtube.com"}; for (int i = 0; i < black.length; i++) { String blackDomain = black[i]; if (domain.lastIndexOf(blackDomain) > 0) { return true; } } return false; } }
|