⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keywordsrank.java

📁 用java写的网站关键字排名的小工具的源码
💻 JAVA
字号:
/*
 * KeyWordsRank.java
 * 查找关键字排名的工具
 * @author jackery qq:41832143
 * @version 1.0
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.j2mehome.tools.searchoptimize;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class KeyWordsRank {
	int pages = 1;

	int which;

	String totalURL;

	String headerURL; // 处理N页查询时用到的临时字符

	String murl; // 要被查的关键字的网址

	// 百度的第一页查询网页前缀
	String baidufirst = "http://www.baidu.com/s?wd=";

	// GG的第一页查询网页前缀和后缀
	String googlefirstbegin = "http://www.google.cn/search?hl=zh-CN&q=";

	String googlefirstend = "&btnG=Google+%E6%90%9C%E7%B4%A2&meta=&aq=-1&oq=";

	// YAHOO的每一页查询网页前缀
	String yahoofirstbegin = "http://search.cn.yahoo.com/search?ei=gbk&fr=fp-tab-web-ycn&pid=ysearch&source=yahoo_yhp_0706_search_button&p=";

	// 百度的第N页查询时的前缀字符串
	String beginbaidu = "http://www.baidu.com/s?lm=0&si=&rn=10&ie=gb2312&ct=0&wd=";

	// GG的第N页查询时的前缀字符串
	String begingoogle = "http://www.google.cn/search?q=";

	// YAHOO的第N页查询时的前缀字符串
	String beginyahoo = "http://search.cn.yahoo.com/search?p=";

	/**
	 * 
	 * @param pages
	 *            要找到多少页
	 * @param which
	 *            要在哪个站查找,1=百度,2=GOOGLE,3=YAHOO
	 */
	public KeyWordsRank(int pages, int which) {
		this.pages = pages;
		this.which = which;

	}

	/*
	 * @param keyword 查排名的关键字 @param murl 要查排名的网站 @return value 反回查到的关键字排名的页数
	 */
	int getPage(String keyword, String murl) {
		switch (which) {
		case 1:
			if (pages == 1) {
				try {
					String enkeyword = java.net.URLEncoder.encode(keyword,
							"GB2312");
					totalURL = baidufirst + enkeyword;
					System.out.print(totalURL);
					System.out.println("\n");
					return getFirstPage(enkeyword, murl, totalURL);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			if (pages != 1) {
				String enkeyword;
				try {
					enkeyword = java.net.URLEncoder.encode(keyword, "GB2312");
					headerURL = beginbaidu + enkeyword;
					return getOtherPage(enkeyword, murl, pages, headerURL,
							which);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			// break;
		case 2:
			if (pages == 1) {
				String enkeyword;
				try {
					enkeyword = java.net.URLEncoder.encode(keyword, "UTF-8");
					totalURL = googlefirstbegin + enkeyword + googlefirstend;
					System.out.println(totalURL);
					return getFirstPage(enkeyword, murl, totalURL);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			if (pages != 1) {
				String enkeyword;
				try {
					enkeyword = java.net.URLEncoder.encode(keyword, "UTF-8");
					headerURL = begingoogle + enkeyword;
					System.out.println(headerURL);
					return getOtherPage(enkeyword, murl, pages, headerURL,
							which);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			// break;
		case 3:
			if (pages == 1) {
				String enkeyword;
				try {
					enkeyword = java.net.URLEncoder.encode(keyword, "GBK");
					totalURL = yahoofirstbegin + enkeyword;
					System.out.println(totalURL);
					return getFirstPage(enkeyword, murl, totalURL);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			if (pages != 1) {
				String enkeyword;
				try {
					enkeyword = java.net.URLEncoder.encode(keyword, "UTF-8");
					headerURL = beginyahoo + enkeyword;
					System.out.println(headerURL);
					return getOtherPage(enkeyword, murl, pages, headerURL,
							which);
				} catch (UnsupportedEncodingException ex) {
					ex.printStackTrace();
				}

			}
			break;
		default:
			return 0;

		}

		return 0;
	}

	/*
	 * 本方法用来得到第一页是否有要找的关键字,只能检查某个引擎的第一页,要想得到位于更深页的关键字排名,请用 getOtherPage()方法.
	 * @param keyword 要查找的关键字 @param murl 关键字所属的网站地址 @param totalURL 要检查的引擎网页网址
	 */
	private int getFirstPage(String keyword, String murl, String totalURL) {
		// System.out.println(keyword);
		// System.out.println(murl);
		// System.out.println(totalURL);

		URL url = null;
		URLConnection conn = null;
		try {
			url = new URL(totalURL);
			try {
				conn = url.openConnection();
				conn.setRequestProperty("User-Agent", "Mozilla/4.0");
				conn.connect();

				InputStreamReader in = new InputStreamReader(conn
						.getInputStream());
				BufferedReader buin = new BufferedReader(in);
				String ln;
				while ((ln = buin.readLine()) != null) {
					if (ln.indexOf(murl) != -1) {
						return 1;
					}
				}
				buin.close();

			} catch (IOException ex) {
				ex.printStackTrace();
			}

		} catch (MalformedURLException ex) {
			ex.printStackTrace();
		}
		return 0;
	}

	/*
	 * 用来得到某一关键字在擎的排名情况,排名大于1页的时候才可以用的
	 * 
	 * 
	 */
	private int getOtherPage(String enkeyword, String murl, int pages,
			String headerURL, int which) {
		String second = null;
		String last = null;
		if (which == 1) {
			second = "&pn=";
			last = "&ver=0&cl=3";
		}
		if (which == 2) {
			second = "&complete=1&hl=zh-CN&newwindow=1&start=";
			last = "&sa=N";
		}
		if (which == 3) {
			second = "&meta=all&pid=ysearch&ei=UTF-8&cns=-1&p4p=0&b=";
			last = "";
		}

		URL url = null;
		URLConnection conn = null;
		for (int i = 1; i < pages; i++) {
			if (which != 3) {
				totalURL = headerURL + second + (i * 10) + last;
				System.out.println(totalURL);
			} else {
				totalURL = headerURL + second + ((i * 10) + 1) + last;
				System.out.println(totalURL);
			}
			try {
				url = new URL(totalURL);
				try {
					conn = url.openConnection();
					conn.setRequestProperty("User-Agent", "Mozilla/4.0");
					conn.connect();
					InputStreamReader in = new InputStreamReader(conn
							.getInputStream());
					BufferedReader buin = new BufferedReader(in);
					String lnn;
					while ((lnn = buin.readLine()) != null) {
						if (lnn.indexOf(murl) != -1) {
							System.out.println("which" + which);
							System.out.println(lnn);
							return i + 1;

						}

					}
					buin.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}

			} catch (MalformedURLException ex) {
				ex.printStackTrace();
			}

		}
		return 0;
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		KeyWordsRank k = new KeyWordsRank(2, 2);
		int ab = k.getPage("eclipse 3.2 下载", "csdn.net");
		System.out.println(ab);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -