您好,登錄后才能下訂單哦!
本篇內容介紹了“用java代碼挖掘長尾關鍵詞的實現方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
長尾關鍵詞(Long Tail Keyword)是指網站上的非目標關鍵詞但與目標關鍵詞相關的也可以帶來搜索流量的組合型關鍵詞。對于做SEO的人來說,這個名字再也熟悉不過了,但是如何來挖掘長尾關鍵詞,對于沒有數據或者一定計算機知識的人來說,還是很有難度的。所以這里附上非常簡單的辦法,只要幾行代碼就能輕松搞定。
# -*- coding: utf-8 -*- # flake8: noqa __author__ = 'wukong' import urllib from urllib import urlencode #配置您申請的appKey和openId app_key="***" open_id="***" """ request_url 請求地址 params 請求參數 method 請求方法 """ def request_content(request_url,params,method): params = urlencode(params) if method and method.lower() =="get": f = urllib.urlopen("%s?%s" % (request_url, params)) else: f = urllib.urlopen(request_url, params) content = f.read() print content def main(): domain="http://api.xiaocongjisuan.com/" servlet="data/longtailword/mining" method="get" request_url=domain+servlet #字典 params ={} params["appKey"]=app_key params["openId"]=open_id #變動部分 params["keyword"]="學前教育" params["upLimit"]=50 params["minLen"]=30 params["lSort"]="up" request_content(request_url,params,method) if __name__ == '__main__': main()
當然也可以用c#來實現
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace ConsoleApplication1 { class Program { private static string appKey="yours"; private static string openId = "yours"; static string getResponseAsString(HttpWebResponse rsp, Encoding encoding) { System.IO.Stream stream = null; StreamReader reader = null; try { // 以字符流的方式讀取HTTP響應 stream = rsp.GetResponseStream(); reader = new StreamReader(stream, encoding); return reader.ReadToEnd(); } finally { // 釋放資源 if (reader != null) reader.Close(); if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } /* * parameters 參數 * encode 編碼 */ static string buildQuery(IDictionary<string,object> parameters, string encode) { StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator<KeyValuePair<string, object>> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value.ToString(); ; // 忽略參數名或參數值為空的參數 if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value) { if (hasParam) { postData.Append("&"); } postData.Append(name); postData.Append("="); if (encode == "gb2312") { postData.Append(System.Web.HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312"))); } else if (encode == "utf8") { postData.Append(System.Web.HttpUtility.UrlEncode(value, Encoding.UTF8)); } else { postData.Append(value); } hasParam = true; } } return postData.ToString(); } /** * * @param url 請求地址 * @param params 請求參數 * @param method 請求方法 * @return 請求結果 * @throws Exception */ static string requestContent(string url, IDictionary<string,object> parameters, string method) { if (method.ToLower() == "post") { HttpWebRequest req = null; HttpWebResponse rsp = null; System.IO.Stream reqStream = null; try { req = (HttpWebRequest)WebRequest.Create(url); req.Method = method; req.KeepAlive = false; req.ProtocolVersion = HttpVersion.Version10; req.Timeout = 5000; req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; byte[] postData = Encoding.UTF8.GetBytes(buildQuery(parameters, "utf8")); reqStream = req.GetRequestStream(); reqStream.Write(postData, 0, postData.Length); rsp = (HttpWebResponse)req.GetResponse(); Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet); return getResponseAsString(rsp, encoding); } catch (Exception ex) { return ex.Message; } finally { if (reqStream != null) reqStream.Close(); if (rsp != null) rsp.Close(); } } else { //創建請求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + buildQuery(parameters, "utf8")); //GET請求 request.Method = "GET"; request.ReadWriteTimeout = 5000; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); //返回內容 string retString = myStreamReader.ReadToEnd(); return retString; } } static void Main(string[] args) { String domain = "http://api.xiaocongjisuan.com/"; String servlet = "data/longtailword/mining"; String method = "get"; String url = domain + servlet; var parameters = new Dictionary<string,object>(); parameters.Add("appKey", appKey); parameters.Add("openId", openId); //變動部分 parameters.Add("keyword", "學前教育"); parameters.Add("upLimit", 50); parameters.Add("minLen", 30); parameters.Add("lSort", "up"); string result = requestContent(url, parameters, method); Console.WriteLine(result); Console.Read(); } } }
其他的語言實現方式可以跳轉到長尾關鍵詞接口去查看,代碼上還是非常簡潔的,就也不再做過多解釋。
“用java代碼挖掘長尾關鍵詞的實現方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。