真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java的爬蟲代碼 爬蟲程序代碼

用java編寫 網(wǎng)絡爬蟲求代碼和流程 急

import java.awt.*;

創(chuàng)新互聯(lián)長期為上千多家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為銅陵企業(yè)提供專業(yè)的網(wǎng)站設計、成都網(wǎng)站制作,銅陵網(wǎng)站改版等技術服務。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.regex.*;

import javax.swing.*;

import javax.swing.table.*;//一個Web的爬行者(注:爬行在這里的意思與抓取,捕獲相同)

public class SearchCrawler extends JFrame{

//最大URL保存值

private static final String[] MAX_URLS={"50","100","500","1000"};

//緩存robot禁止爬行列表

private HashMap disallowListCache=new HashMap();

//搜索GUI控件

private JTextField startTextField;

private JComboBox maxComboBox;

private JCheckBox limitCheckBox;

private JTextField logTextField;

private JTextField searchTextField;

private JCheckBox caseCheckBox;

private JButton searchButton;

//搜索狀態(tài)GUI控件

private JLabel crawlingLabel2;

private JLabel crawledLabel2;

private JLabel toCrawlLabel2;

private JProgressBar progressBar;

private JLabel matchesLabel2;

//搜索匹配項表格列表

private JTable table;

//標記爬行機器是否正在爬行

private boolean crawling;

//寫日志匹配文件的引用

private PrintWriter logFileWriter;

//網(wǎng)絡爬行者的構造函數(shù)

public SearchCrawler(){

//設置應用程序標題欄

setTitle("搜索爬行者");

//設置窗體大小

setSize(600,600);

//處理窗體關閉事件

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

actionExit();

}

});

//設置文件菜單

JMenuBar menuBar=new JMenuBar();

JMenu fileMenu=new JMenu("文件");

fileMenu.setMnemonic(KeyEvent.VK_F);

JMenuItem fileExitMenuItem=new JMenuItem("退出",KeyEvent.VK_X);

fileExitMenuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

actionExit();

}

});

fileMenu.add(fileExitMenuItem);

menuBar.add(fileMenu);

setJMenuBar(menuBar);

如何用Java寫一個爬蟲

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

import java.io.File;

import java.net.URL;

import java.net.URLConnection;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Scanner;

import java.util.UUID;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class DownMM {

public static void main(String[] args) throws Exception {

//out為輸出的路徑,注意要以\\結尾

String out = "D:\\JSP\\pic\\java\\";

try{

File f = new File(out);

if(! f.exists()) {

f.mkdirs();

}

}catch(Exception e){

System.out.println("no");

}

String url = "-";

Pattern reg = Pattern.compile("img src=\"(.*?)\"");

for(int j=0, i=1; i=10; i++){

URL uu = new URL(url+i);

URLConnection conn = uu.openConnection();

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");

Scanner sc = new Scanner(conn.getInputStream());

Matcher m = reg.matcher(sc.useDelimiter("\\A").next());

while(m.find()){

Files.copy(new URL(m.group(1)).openStream(), Paths.get(out + UUID.randomUUID() + ".jpg"));

System.out.println("已下載:"+j++);

}

}

}

Java網(wǎng)絡爬蟲怎么實現(xiàn)?

網(wǎng)絡爬蟲是一個自動提取網(wǎng)頁的程序,它為搜索引擎從萬維網(wǎng)上下載網(wǎng)頁,是搜索引擎的重要組成。

傳統(tǒng)爬蟲從一個或若干初始網(wǎng)頁的URL開始,獲得初始網(wǎng)頁上的URL,在抓取網(wǎng)頁的過程中,不斷從當前頁面上抽取新的URL放入隊列,直到滿足系統(tǒng)的一定停止條件。對于垂直搜索來說,聚焦爬蟲,即有針對性地爬取特定主題網(wǎng)頁的爬蟲,更為適合。

以下是一個使用java實現(xiàn)的簡單爬蟲核心代碼:

public void crawl() throws Throwable {

while (continueCrawling()) {

CrawlerUrl url = getNextUrl(); //獲取待爬取隊列中的下一個URL

if (url != null) {

printCrawlInfo();

String content = getContent(url); //獲取URL的文本信息

//聚焦爬蟲只爬取與主題內(nèi)容相關的網(wǎng)頁,這里采用正則匹配簡單處理

if (isContentRelevant(content, this.regexpSearchPattern)) {

saveContent(url, content); //保存網(wǎng)頁至本地

//獲取網(wǎng)頁內(nèi)容中的鏈接,并放入待爬取隊列中

Collection urlStrings = extractUrls(content, url);

addUrlsToUrlQueue(url, urlStrings);

} else {

System.out.println(url + " is not relevant ignoring ...");

}

//延時防止被對方屏蔽

Thread.sleep(this.delayBetweenUrls);

}

}

closeOutputStream();

}

private CrawlerUrl getNextUrl() throws Throwable {

CrawlerUrl nextUrl = null;

while ((nextUrl == null) (!urlQueue.isEmpty())) {

CrawlerUrl crawlerUrl = this.urlQueue.remove();

//doWeHavePermissionToVisit:是否有權限訪問該URL,友好的爬蟲會根據(jù)網(wǎng)站提供的"Robot.txt"中配置的規(guī)則進行爬取

//isUrlAlreadyVisited:URL是否訪問過,大型的搜索引擎往往采用BloomFilter進行排重,這里簡單使用HashMap

//isDepthAcceptable:是否達到指定的深度上限。爬蟲一般采取廣度優(yōu)先的方式。一些網(wǎng)站會構建爬蟲陷阱(自動生成一些無效鏈接使爬蟲陷入死循環(huán)),采用深度限制加以避免

if (doWeHavePermissionToVisit(crawlerUrl)

(!isUrlAlreadyVisited(crawlerUrl))

isDepthAcceptable(crawlerUrl)) {

nextUrl = crawlerUrl;

// System.out.println("Next url to be visited is " + nextUrl);

}

}

return nextUrl;

}

private String getContent(CrawlerUrl url) throws Throwable {

//HttpClient4.1的調用與之前的方式不同

HttpClient client = new DefaultHttpClient();

HttpGet httpGet = new HttpGet(url.getUrlString());

StringBuffer strBuf = new StringBuffer();

HttpResponse response = client.execute(httpGet);

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

HttpEntity entity = response.getEntity();

if (entity != null) {

BufferedReader reader = new BufferedReader(

new InputStreamReader(entity.getContent(), "UTF-8"));

String line = null;

if (entity.getContentLength() 0) {

strBuf = new StringBuffer((int) entity.getContentLength());

while ((line = reader.readLine()) != null) {

strBuf.append(line);

}

}

}

if (entity != null) {

nsumeContent();

}

}

//將url標記為已訪問

markUrlAsVisited(url);

return strBuf.toString();

}

public static boolean isContentRelevant(String content,

Pattern regexpPattern) {

boolean retValue = false;

if (content != null) {

//是否符合正則表達式的條件

Matcher m = regexpPattern.matcher(content.toLowerCase());

retValue = m.find();

}

return retValue;

}

public List extractUrls(String text, CrawlerUrl crawlerUrl) {

Map urlMap = new HashMap();

extractHttpUrls(urlMap, text);

extractRelativeUrls(urlMap, text, crawlerUrl);

return new ArrayList(urlMap.keySet());

}

private void extractHttpUrls(Map urlMap, String text) {

Matcher m = (text);

while (m.find()) {

String url = m.group();

String[] terms = url.split("a href=\"");

for (String term : terms) {

// System.out.println("Term = " + term);

if (term.startsWith("http")) {

int index = term.indexOf("\"");

if (index 0) {

term = term.substring(0, index);

}

urlMap.put(term, term);

System.out.println("Hyperlink: " + term);

}

}

}

}

private void extractRelativeUrls(Map urlMap, String text,

CrawlerUrl crawlerUrl) {

Matcher m = relativeRegexp.matcher(text);

URL textURL = crawlerUrl.getURL();

String host = textURL.getHost();

while (m.find()) {

String url = m.group();

String[] terms = url.split("a href=\"");

for (String term : terms) {

if (term.startsWith("/")) {

int index = term.indexOf("\"");

if (index 0) {

term = term.substring(0, index);

}

String s = //" + host + term;

urlMap.put(s, s);

System.out.println("Relative url: " + s);

}

}

}

}

public static void main(String[] args) {

try {

String url = "";

Queue urlQueue = new LinkedList();

String regexp = "java";

urlQueue.add(new CrawlerUrl(url, 0));

NaiveCrawler crawler = new NaiveCrawler(urlQueue, 100, 5, 1000L,

regexp);

// boolean allowCrawl = crawler.areWeAllowedToVisit(url);

// System.out.println("Allowed to crawl: " + url + " " +

// allowCrawl);

crawler.crawl();

} catch (Throwable t) {

System.out.println(t.toString());

t.printStackTrace();

}

}

Java源碼 實現(xiàn)網(wǎng)絡爬蟲?

//Java爬蟲demo

import?java.io.File;

import?java.net.URL;

import?java.net.URLConnection;

import?java.nio.file.Files;

import?java.nio.file.Paths;

import?java.util.Scanner;

import?java.util.UUID;

import?java.util.regex.Matcher;

import?java.util.regex.Pattern;

public?class?DownMM?{

public?static?void?main(String[]?args)?throws?Exception?{

//out為輸出的路徑,注意要以\\結尾

String?out?=?"D:\\JSP\\pic\\java\\";?

try{

File?f?=?new?File(out);

if(!?f.exists())?{??

f.mkdirs();??

}??

}catch(Exception?e){

System.out.println("no");

}

String?url?=?"-";

Pattern?reg?=?Pattern.compile("img?src=\"(.*?)\"");

for(int?j=0,?i=1;?i=10;?i++){

URL?uu?=?new?URL(url+i);

URLConnection?conn?=?uu.openConnection();

conn.setRequestProperty("User-Agent",?"Mozilla/5.0?(Windows?NT?6.3;?WOW64;?Trident/7.0;?rv:11.0)?like?Gecko");

Scanner?sc?=?new?Scanner(conn.getInputStream());

Matcher?m?=?reg.matcher(sc.useDelimiter("\\A").next());

while(m.find()){

Files.copy(new?URL(m.group(1)).openStream(),?Paths.get(out?+?UUID.randomUUID()?+?".jpg"));

System.out.println("已下載:"+j++);

}

}

}

}


網(wǎng)站欄目:java的爬蟲代碼 爬蟲程序代碼
文章鏈接:http://weahome.cn/article/hphcod.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部