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

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

java實(shí)現(xiàn)爬蟲代碼 Java爬蟲代碼

如何用java爬蟲爬取招聘信息

1、思路:

肥城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

明確需要爬取的信息

分析網(wǎng)頁結(jié)構(gòu)

分析爬取流程

優(yōu)化

2、明確需要爬取的信息

職位名稱

工資

職位描述

公司名稱

公司主頁

詳情網(wǎng)頁

分析網(wǎng)頁結(jié)構(gòu)

3、目標(biāo)網(wǎng)站-拉勾網(wǎng)

網(wǎng)站使用json作為交互數(shù)據(jù),分析json數(shù)據(jù),需要的json關(guān)鍵數(shù)據(jù)

查看需要的信息所在的位置,使用Jsoup來解析網(wǎng)頁

4、分析爬取流程

1.獲取所有的positionId生成詳情頁,存放在一個(gè)存放網(wǎng)址列表中ListString joburls

2.獲取每個(gè)詳情頁并解析為Job類,得到一個(gè)存放Job類的列表ListJob jobList

3.把ListJob jobList存進(jìn)Excel表格中

Java操作Excel需要用到j(luò)xl

5、關(guān)鍵代碼實(shí)現(xiàn)

public ListString getJobUrls(String gj,String city,String kd){

String pre_url="";

String end_url=".html";

String url;

if (gj.equals("")){

url=";city="+city+"needAddtionalResult=falsefirst=falsepn="+pn+"kd="+kd;

}else {

url=""+gj+"px=defaultcity="+city+"needAddtionalResult=falsefirst=falsepn="+pn+"kd="+kd;

}

String rs=getJson(url);

System.out.println(rs);

int total= JsonPath.read(rs,"$.content.positionResult.totalCount");//獲取總數(shù)

int pagesize=total/15;

if (pagesize=30){

pagesize=30;

}

System.out.println(total);

// System.out.println(rs);

ListInteger posid=JsonPath.read(rs,"$.content.positionResult.result[*].positionId");//獲取網(wǎng)頁id

for (int j=1;j=pagesize;j++){ //獲取所有的網(wǎng)頁id

pn++; //更新頁數(shù)

url=""+gj+"px=defaultcity="+city+"needAddtionalResult=falsefirst=falsepn="+pn+"kd="+kd;

String rs2=getJson(url);

ListInteger posid2=JsonPath.read(rs2,"$.content.positionResult.result[*].positionId");

posid.addAll(posid2); //添加解析的id到第一個(gè)list

}

ListString joburls=new ArrayList();

//生成網(wǎng)頁列表

for (int id:posid){

String url3=pre_url+id+end_url;

joburls.add(url3);

}

return joburls;

}

public Job getJob(String url){ //獲取工作信息

Job job=new Job();

Document document= null;

document = Jsoup.parse(getJson(url));

job.setJobname(document.select(".name").text());

job.setSalary(document.select(".salary").text());

String joball=HtmlTool.tag(document.select(".job_bt").select("div").html());//清除html標(biāo)簽

job.setJobdesc(joball);//職位描述包含要求

job.setCompany(document.select(".b2").attr("alt"));

Elements elements=document.select(".c_feature");

//System.out.println(document.select(".name").text());

job.setCompanysite(elements.select("a").attr("href")); //獲取公司主頁

job.setJobdsite(url);

return job;

}

void insertExcel(ListJob jobList) throws IOException, BiffException, WriteException {

int row=1;

Workbook wb = Workbook.getWorkbook(new File(JobCondition.filename));

WritableWorkbook book = Workbook.createWorkbook(new File(JobCondition.filename), wb);

WritableSheet sheet=book.getSheet(0);

for (int i=0;ijobList.size();i++){ //遍歷工作列表,一行行插入到表格中

sheet.addCell(new Label(0,row,jobList.get(i).getJobname()));

sheet.addCell(new Label(1,row,jobList.get(i).getSalary()));

sheet.addCell(new Label(2,row,jobList.get(i).getJobdesc()));

sheet.addCell(new Label(3,row,jobList.get(i).getCompany()));

sheet.addCell(new Label(4,row,jobList.get(i).getCompanysite()));

sheet.addCell(new Label(5,row,jobList.get(i).getJobdsite()));

row++;

}

book.write();

book.close();

}

如何用Java寫一個(gè)爬蟲

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為輸出的路徑,注意要以\\結(jié)尾

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)絡(luò)爬蟲求代碼和流程 急

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.regex.*;

import javax.swing.*;

import javax.swing.table.*;//一個(gè)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;

//搜索匹配項(xiàng)表格列表

private JTable table;

//標(biāo)記爬行機(jī)器是否正在爬行

private boolean crawling;

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

private PrintWriter logFileWriter;

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

public SearchCrawler(){

//設(shè)置應(yīng)用程序標(biāo)題欄

setTitle("搜索爬行者");

//設(shè)置窗體大小

setSize(600,600);

//處理窗體關(guān)閉事件

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

actionExit();

}

});

//設(shè)置文件菜單

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網(wǎng)絡(luò)爬蟲怎么實(shí)現(xiàn)?

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

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

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

public void crawl() throws Throwable {

while (continueCrawling()) {

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

if (url != null) {

printCrawlInfo();

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

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

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

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

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

Collection urlStrings = extractUrls(content, url);

addUrlsToUrlQueue(url, urlStrings);

} else {

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

}

//延時(shí)防止被對(duì)方屏蔽

Thread.sleep(this.delayBetweenUrls);

}

}

closeOutputStream();

}

private CrawlerUrl getNextUrl() throws Throwable {

CrawlerUrl nextUrl = null;

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

CrawlerUrl crawlerUrl = this.urlQueue.remove();

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

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

//isDepthAcceptable:是否達(dá)到指定的深度上限。爬蟲一般采取廣度優(yōu)先的方式。一些網(wǎng)站會(huì)構(gòu)建爬蟲陷阱(自動(dò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的調(diào)用與之前的方式不同

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標(biāo)記為已訪問

markUrlAsVisited(url);

return strBuf.toString();

}

public static boolean isContentRelevant(String content,

Pattern regexpPattern) {

boolean retValue = false;

if (content != null) {

//是否符合正則表達(dá)式的條件

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 網(wǎng)絡(luò)爬蟲怎么實(shí)現(xiàn)?

1、在打開的ie瀏覽器窗口右上方點(diǎn)擊齒輪圖標(biāo),選擇“Internet選項(xiàng)”,如下圖所示:

2、在打開的Internet選項(xiàng)窗口中,切換到安全欄,在安全選卡中點(diǎn)擊“自定義級(jí)別”,如下圖所示:

3、在“安全設(shè)置-Internet區(qū)域”界面找到“Java小程序腳本”、“活動(dòng)腳本”,并將這兩個(gè)選項(xiàng)都選擇為“禁用”,然后點(diǎn)擊確定,如下圖所示:

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

//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為輸出的路徑,注意要以\\結(jié)尾

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實(shí)現(xiàn)爬蟲代碼 Java爬蟲代碼
文章起源:http://weahome.cn/article/hphgjc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部