发现更大的SEO世界
 找回密码
 注册
搜索
»首页»SEO培训 SEO论坛 SEO交流 帖子
发新帖
波波,seo爱好者    

Java 实现的关键词批量查询排名和计算平均排名

本帖最后由 波波 于 2014-6-21 13:11 编辑

    论坛里很多人都是用python写一些SEO的小工具,自己也学习了python半个多月,掌握了基本的语法结构,但是用起来还是不得心应手。于是用自己比较熟悉的Java语言写了一个小工具,批量查询网站关键词排名和计算平均排名。不过有个前提就是有自己的关键词词库,我这里是从文本文件里面读取关键词的,然后去百度查询关键词排名。这里用的搜外网站做测试,只计算了排名前20的,其中的平均排名会比实际排名要高,对于超过20的都是按照排名20处理,不过都是为了测试功能。
     代码还有些问题,就是查询的时候,会查询超时,目前在考虑怎么解决。但效率问题还是有待提升,等优化好了会再一次贴出代码,这里先放出测试版,有兴趣的同学可以一起讨论,给意见。

  1. package bb.com;

  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.regex.Pattern;
  9. import org.jsoup.Jsoup;
  10. import org.jsoup.nodes.Document;

  11. public class Test {
  12.         public static long time = 0;
  13.         public static int sum = 0;
  14.         public static String getContentByJsoup(String url) {   //查询关键词排名
  15.                 String content = "";
  16.                 try {
  17.                         Date startdate = new Date();
  18.                         Document doc = Jsoup.connect(url)
  19.                                         .timeout(5000)
  20.                                         .get();
  21.                         Date enddate = new Date();
  22.                         long t = enddate.getTime() - startdate.getTime();
  23.                         time += t;
  24.                         Iterator it = doc.select("span.g,span.c-showurl").iterator();
  25.                         String str = "seowhy.com";
  26.                         String regex = "<([^>]*)>";
  27.                         Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  28.                         int i = 1;
  29.                         while (it.hasNext()) {
  30.                                 String s = pattern.matcher(it.next().toString()).replaceAll("");
  31.                                 if (s.contains(str)) {
  32.                                         content = " 排名   " + i;
  33.                                         sum += i;
  34.                                        
  35.                                         break;
  36.                                 }
  37.                                 i++;
  38.                         }
  39.                         if (content.equals("")) {
  40.                                 content = " 排名20名以外";
  41.                                
  42.                                 sum += 20;
  43.                         }
  44.                 } catch (IOException e) {
  45.                         System.out.println("查询超时!!");
  46.                        
  47.                 }
  48.                 return content;
  49.         }

  50.         public static int readkeyfromfile() throws IOException {            //从文件中读取关键词
  51.                 BufferedReader br = new BufferedReader(new InputStreamReader(
  52.                                 new FileInputStream("E:/workspace/key.txt"), "UTF-8"));
  53.                 String line = "";
  54.                 int i = 0;
  55.                 String url = "http://www.baidu.com/s?wd=";
  56.                 String u = "";
  57.                 System.out.println("seowhy关键词排名情况:");
  58.                 while ((line = br.readLine()) != null) {
  59.                         u = url + line + "&rn=20";
  60.                         System.out.println(line + " " + getContentByJsoup(u));
  61.                         i++;
  62.                 }
  63.                 br.close();
  64.                 return i;

  65.         }

  66.         public static void main(String args[]) throws IOException {
  67.                 int keys = readkeyfromfile();
  68.                 System.out.println("关键词个数:" + keys);
  69.                 System.out.println("平均排名:" + sum / keys);
  70.                 System.out.println("耗时==" + time);
  71.         }

  72. }
复制代码

以下是运行结果:


评分

参与人数 4赞同 +16 收起 理由
莫山 + 4 赞一个!
丶小苞 + 4 赞一个!
vic + 4 虽然我不用java但觉得应该是比较有用的 感.
行书 + 4 类似功能的帖子蛮多的,最后显示的结果最好.

查看全部评分

发表于 2014-6-21 13:04:21 |只看大图
回复 收藏
波波,seo爱好者    

本帖最后由 波波 于 2014-6-21 17:25 编辑

解决关键词查询超时问题
之前用jsoup去请求连接,现在改成了用httpclient去请求连接,然后有jsoup解析html

  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.URLEncoder;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.regex.Pattern;
  9. import org.apache.commons.httpclient.HttpClient;
  10. import org.apache.commons.httpclient.HttpException;
  11. import org.apache.commons.httpclient.HttpStatus;
  12. import org.apache.commons.httpclient.methods.GetMethod;
  13. import org.jsoup.Jsoup;
  14. import org.jsoup.nodes.Document;

  15. public class Down {

  16.     public static long time = 0;
  17.         public static int sum = 0;
  18.     public static String downweb(String url){
  19.             Date startdate = new Date();
  20.         HttpClient client = new HttpClient();  
  21.         GetMethod getMethod = new GetMethod(url);
  22.         getMethod.getParams().setContentCharset("utf-8");
  23.         String ts="";
  24.         try {  
  25.                
  26.             int statusCode = client.executeMethod(getMethod);  
  27.              if (statusCode != HttpStatus.SC_OK) {  
  28.                     System.err.println("加载失败" + getMethod.getStatusLine());  
  29.                   }  
  30.              BufferedReader reader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));
  31.              StringBuffer stringBuffer = new StringBuffer();
  32.              String str = "";
  33.              while((str = reader.readLine())!=null){
  34.                      stringBuffer.append(str);
  35.              }
  36.               ts = stringBuffer.toString();
  37.       
  38.         } catch (HttpException e) {  
  39.               e.printStackTrace();  
  40.       
  41.         } catch (IOException e) {  
  42.               e.printStackTrace();  
  43.       
  44.         }finally{  
  45.                 getMethod.releaseConnection();  
  46.       
  47.         }  
  48.         Date enddate = new Date();
  49.         long t = enddate.getTime() - startdate.getTime();
  50.         time += t;
  51.             return ts;
  52.     }

  53.         public static String getContentByJsoup(String url) {
  54.                 String content = "";
  55.                 Document doc = Jsoup.parse(downweb(url));
  56.                 Iterator it = doc.select("span.g,span.c-showurl").iterator();
  57.                 String str = "seowhy.com";
  58.                 String regex = "<([^>]*)>";
  59.                 Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  60.                 int i = 1;
  61.                 while (it.hasNext()) {
  62.                         String s = pattern.matcher(it.next().toString()).replaceAll("");
  63.                         if (s.contains(str)) {
  64.                                 content = "排名   " + i;
  65.                                 sum += i;
  66.                                 break;
  67.                         }
  68.                         i++;
  69.                 }
  70.                 if (content.equals("")) {
  71.                         content = "排名20名以外";
  72.                        
  73.                         sum += 20;
  74.                 }
  75.                 return content;
  76.         }

  77.         public static int readkeyfromfile() throws IOException {
  78.                 BufferedReader br = new BufferedReader(new InputStreamReader(
  79.                                 new FileInputStream("E:/workspace/key.txt"), "UTF-8"));
  80.                 String line = "";
  81.                 int i = 0;
  82.                 String url = "http://www.baidu.com/s?wd=";
  83.                 String u = "";
  84.                 String str="";
  85.                 String s="";
  86.                 System.out.println("seowhy关键词排名情况:");
  87.                 while ((line = br.readLine()) != null) {
  88.                          str=URLEncoder.encode(line,"utf-8");
  89.                          u = url + str + "&rn=20";
  90.                          s = line+"      "+getContentByJsoup(u);
  91.                          System.out.println(s);
  92.                         i++;
  93.                 }
  94.                 br.close();
  95.                 return i;

  96.         }
  97.     public static void main(String args[]) throws IOException{  
  98.           int keys = readkeyfromfile();
  99.         System.out.println("关键词个数:" + keys);
  100.         System.out.println("平均排名:" + sum / keys);
  101.         System.out.println("耗时==" + time);
  102.     }  
  103. }
复制代码
 楼主| 发表于 2014-6-21 17:23:09
回复 收藏
ZERO,SEO执着爱好者    

推荐早日投入Python怀抱。

随便看到的一小段代码:
s.contains(str)

Java还算比较简洁,PHP版是:
strpos(s, str)!==false

PHP还不能用!=false,可能会出错。因为str出现在起始的时候值是0,没找到的时候值是false,!=false会混淆两者,一定要限定变量类型相同。

连这么个常用功能,都要纠结程序角度的概念,实在是太过于繁琐。

我当初放弃PHP改学Python,其中一部分原因就是这类情况。

Python版非常简单而且符合日常思维:
str in s

str是否在s里面,超级简单。

感觉这才是给人用的编程语言。
发表于 2014-6-21 21:46:48
回复 收藏
波波,seo爱好者    

ZERO 发表于 2014-6-21 21:46
推荐早日投入Python怀抱。

随便看到的一小段代码:

呵呵,Python目前在看,还不熟练,不过我确实是感觉到python在语法方面很简洁
 楼主| 发表于 2014-6-22 09:28:01
回复 收藏
丶小苞,小孩子还在玩泥沙    

我提一个小小的建议,得出结果是否能够排列整齐些?这样看起来很乱。
发表于 2014-6-22 16:55:09
回复 收藏
波波,seo爱好者    

丶小苞 发表于 2014-6-22 16:55
我提一个小小的建议,得出结果是否能够排列整齐些?这样看起来很乱。

恩恩,注意到了,改了后会贴出来
 楼主| 发表于 2014-6-22 19:33:44
回复 收藏
波波,seo爱好者    

改正了输出结果对齐,尝试了制表符,不过没起效果,后来取了个巧,不过这个方法应该不能适用所有,没测试,对目前的数据有效果,不过关键词中有空格不生效,不管是在关键词前后还是中间
改了readkeyfromfile函数
  1. public static int readkeyfromfile() throws IOException {
  2.                 BufferedReader br = new BufferedReader(new InputStreamReader(
  3.                                 new FileInputStream("E:/workspace/key.txt"), "UTF-8"));
  4.                 String line = "";
  5.                 int i = 0;
  6.                 String url = "http://www.baidu.com/s?wd=";
  7.                 String u = "";
  8.                 String str="";
  9.                 String s="";
  10.                 System.out.println("seowhy关键词排名情况:");
  11.                 while ((line = br.readLine()) != null) {
  12.                          str=URLEncoder.encode(line,"utf-8");
  13.                          u = url + str + "&rn=20";
  14.                          if(line.length()<6){
  15.                                  s = line+"\t\t\t"+getContentByJsoup(u);
  16.                          }else{
  17.                                  s = line+"\t\t"+getContentByJsoup(u);
  18.                          }
  19.                          
  20.                          System.out.println(s);
  21.                         i++;
  22.                 }
  23.                 br.close();
  24.                 return i;

  25.         }
复制代码


 楼主| 发表于 2014-6-23 14:41:30
回复 收藏
快速回复 返回顶部 返回列表