獲得網站在百度關鍵字搜尋的排名
阿新 • • 發佈:2019-01-27
/**
* @Title t.java
* @Package test
* @Description TODO(用一句話描述該檔案做什麼)
* @author zqs
* @date 2013年11月28日 上午9:51:05
* @version V1.0
*/
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class t {
public static String request(String url) {
StringBuffer res = new StringBuffer();
HttpURLConnection conn = null;
try {
URL serverUrl = new URL(url);
conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
InputStream ins = conn.getInputStream();
String charset = "UTF-8";
InputStreamReader inr = new InputStreamReader(ins, charset);
BufferedReader bfr = new BufferedReader(inr);
String line = "";
do {
res.append(line);
line = bfr.readLine();
} while (line != null);
inr.close();
bfr.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return res.toString();
}
public static void main(String[] args) throws Exception {
String keyword = "CRM";
String site = "metasoft";
keyword = URLEncoder.encode(keyword, "utf-8");
int p = 1;
int s = 10;
String url = "http://www.baidu.com/s?wd=%s&pn=%s&ie=utf-8&usm=1&rsv_page=1";
while (true) {
int pn = (p - 1) * s;
String _url = String.format(url, keyword, pn + "");
System.out.println("Request url " + _url);
String html = request(_url);
if (html.indexOf(site) != -1) {
System.out.println("Find keyword");
break;
}
p++;
}
}
}
* @Title t.java
* @Package test
* @Description TODO(用一句話描述該檔案做什麼)
* @author zqs
* @date 2013年11月28日 上午9:51:05
* @version V1.0
*/
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class t {
public static String request(String url) {
StringBuffer res = new StringBuffer();
HttpURLConnection conn = null;
try {
URL serverUrl = new URL(url);
conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
InputStream ins = conn.getInputStream();
String charset = "UTF-8";
InputStreamReader inr = new InputStreamReader(ins, charset);
BufferedReader bfr = new BufferedReader(inr);
String line = "";
do {
res.append(line);
line = bfr.readLine();
} while (line != null);
inr.close();
bfr.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return res.toString();
}
public static void main(String[] args) throws Exception {
String keyword = "CRM";
String site = "metasoft";
keyword = URLEncoder.encode(keyword, "utf-8");
int p = 1;
int s = 10;
String url = "http://www.baidu.com/s?wd=%s&pn=%s&ie=utf-8&usm=1&rsv_page=1";
while (true) {
int pn = (p - 1) * s;
String _url = String.format(url, keyword, pn + "");
System.out.println("Request url " + _url);
String html = request(_url);
if (html.indexOf(site) != -1) {
System.out.println("Find keyword");
break;
}
p++;
}
}
}
速度有點慢,執行多少行即為百度的排名值