Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The implementation of Mining long-tailed keywords with java Code

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "using java code to mine long-tailed keywords". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Long-tailed keywords (Long Tail Keyword) refers to the combination of non-target keywords on the site, but related to the target keywords can also bring search traffic. For people who do SEO, the name is all too familiar, but how to mine long-tailed keywords is still very difficult for people who do not have data or certain computer knowledge. So here is a very simple solution that can be easily done with just a few lines of code.

#-*-coding: utf-8-*-# flake8: noqa__author__ = 'wukong'import urllibfrom urllib import urlencode# configure the appKey and openIdapp_key= you applied for "*" open_id= "* *" request_url request address params request parameters method request method "def request_content (request_url,params)" Method): params = urlencode (params) if method and method.lower () = "get": F = urllib.urlopen ("% slots% 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 # dictionary params = {} params [" appKey "] = app_key params [" openId "] = open_id # variable params [" keyword "] =" preschool "params [" UpLimit "] = 50 params [" minLen "] = 30 params [" lSort "] =" up "request_content (request_url Params,method) if _ _ name__ = ='_ main__': main ()

Of course, it can also be realized with 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 {/ / read HTTP response in character stream stream = rsp.GetResponseStream (); reader = new StreamReader (stream, encoding); return reader.ReadToEnd () } finally {/ / release resources if (reader! = null) reader.Close (); if (stream! = null) stream.Close (); if (rsp! = null) rsp.Close () }} / * * parameters parameter * encode Encoding * / static string buildQuery (IDictionary parameters, string encode) {StringBuilder postData = new StringBuilder (); bool hasParam = false; IEnumerator dem = parameters.GetEnumerator () While (dem.MoveNext ()) {string name = dem.Current.Key; string value = dem.Current.Value.ToString (); / / ignore parameter if (! string.IsNullOrEmpty (name)) / / & &! string.IsNullOrEmpty (value) {if (hasParam) {postData.Append ("&") if the parameter name or parameter value is empty. } 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 request address * @ param params request parameters * @ param method request method * @ return request result * @ throws Exception * / static string requestContent (string url, IDictionary 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 {/ / create request HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url + "?" + buildQuery (parameters, "utf8")); / / GET request 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")); / / return content 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 (); parameters.Add (" appKey ", appKey) Parameters.Add ("openId", openId); / / variable part parameters.Add ("keyword", "preschool"); parameters.Add ("upLimit", 50); parameters.Add ("minLen", 30); parameters.Add ("lSort", "up"); string result = requestContent (url, parameters, method) Console.WriteLine (result); Console.Read ();}

Other language implementations can jump to the long-tailed keyword interface to view, the code is still very concise, it will no longer do too much explanation.

This is the end of the content of "how to mine long-tailed keywords with java code". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report