在网站扫描领域网站爬虫
发布时间: 2020-09-11 13:56:40文章作者: 网站编辑阅读量: 225
网站爬虫 由于是针对网站应用进行的扫描,所以利用爬虫遍历网站中的所有页面就变得必不可少了。在网站扫描领域,利用爬虫主要是希望对网站的资产能够进行全面了解和梳理,因为在多数情况下,我们并不需要把网站的页面内容抓取下来,只要能够遍历所有的页面即可。
在网页遍历过程的某些场景下,还需要进行登录扫描,主要是因为在企业网站中,有些页面只有在成功登录后才能访问到。常见的网站登录扫描方式有两种,一种是把在登录页面中需要填写堤交的内容记录下来,然后模拟登录过程,另一种是把登录后的Cookie提取出来,用作登录凭证。
有关爬虫技术的相关文档及自动化的工具大家可以自行了解,此处不再盩述。下面提供一^简单的爬虫。
import
import
import
import
import
import
java.io.lOException;
j ava. r.e t. SocJcetTimecutException;
java.net.URI;
java.net.URISyntaxException;
j ava. r.e t. UnkncwnHos tExceprion;
j ava.urii.ArrayList;
import org.j soup.HttpStatusExceprion;
import org.jsoup.Jsoup;
import org. j soup. Unsuppcrt ed2*Iiir.eTypeExceprion;
irt^ort org.jsoup.nodes.Document;
irt^ort org.jsoup.nodes.Element;
irt^ort org. jsoup. select .Elements;
public class Crawler {
static String initial_uri_string = "http://new3.sina.com.cn'*;
static String domain = M3ina.com.cn";
public static void main(String[] arg3) {
URI newURI;
URI baseUR: = null;
int index = 0;
String uriString;
ArrayList<StrLng> uriStringAL = new Array二ist<String>{);
uriStringAL.add(initial uri string);
do {
uriString = uriStringAL.ger(index++);
System.out.println(M (M + index + ??/?’ + uriStringAL.size () +
?,)Crawling _,+ uriString + _,. .. M);
try {
baseURI = new URI(uriString);
Document doc =
Jsoup.connect(uriString).userAgent("Mozilla/S.0").get();
Elements es = doc.getElementsByTag("a");
for (Element e:es) {
newURI = :ba3eURI.re3〇lve{e.attr{"href") ) ,?
if (newURI.getHostO .end3With(dcir.ain)) {
if (!uriStringAL.scntains(newUR二?toString()} s&
! nev>?URI. toString 〇 . isEmpty () ) {
uriStringAL?add(newUR二?toString()};
}
}
}
} catch (SocketTimeoutException e) {
Sy3teir..our .println {',Soc■cetTi3[leoutExcep'Cion,,);
} catch (Un-cnov/nKcstException e) {
Sy3teir..our .println {"UnJcnownHcstException");
} catch {IllegaLArgumentSxsep'Cion e> {
System.out .println二llegalArgurcientException");
} catch (HttpStatusException e) {
System, our .println {''HttpStatusException");
} catch (UnsupportedMimeTypeZxception e) {
System, our .println {',UnsuppcrtedMiir.erypeExceprionM);
} catch {lOExseption e) {
System, out .println 厂二CException’’};
} catch (URISyntaxSxseption e> {
System, out .println {?’URISyntaxExseption”);
} catch (NuliPointerException e) {
System.our.println{"NullPointerExceution");
}
} while (uriStringAl.size() > index);
}
}