Skip to content

Instantly share code, notes, and snippets.

@atechcrew
Last active July 9, 2018 16:23
Show Gist options
  • Save atechcrew/61efbbc43a33a351e6c07d6c5222a9eb to your computer and use it in GitHub Desktop.
Save atechcrew/61efbbc43a33a351e6c07d6c5222a9eb to your computer and use it in GitHub Desktop.
Keyword Suggestion tool in java. Java code to make your own simple keyword suggestion tool. Make your own keyword suggestion tool in java.
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Random;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class KeywordSuggestion {
static ArrayList<String> suggestionsList = new ArrayList<String>();
public static void main(String args[]) {
int number = 3; // Number of depths to go and find keywords
findSuggestions("facebook covers", number);
try {
Thread.sleep(5000);
while(true) {
Thread.sleep(1000);
//wait until fetching is complete (thread count will be 1 when all threads complete their tasks)
if(Thread.activeCount() == 1) {
//Stop waiting when fetching is completed
break;
}
}
for(int i = 0; i < suggestionsList.size(); i++) {
System.out.println((i+1) + "=>\t"+suggestionsList.get(i));
}
System.out.println();
System.out.println("Total Number of keywords fetched: " + suggestionsList.size());
}catch(Exception e) {}
}
public static void findSuggestions(String focuskeyword, int depth){
Runnable run = new Runnable() {
@Override
public void run() {
if(depth <= -1){
return;
}
String keyword = focuskeyword.trim().replaceAll(" ", "+");
try {
Random rand = new SecureRandom();
int requestTime = rand.nextInt(10000) + 15000;
String request = "http://www.bing.com/search?q="+ keyword +"&go=Submit&qs=n&form=QBLH&pq="+ keyword +"&sc=8-5&sp=-1&sk=&cvid=8ACF40D43BE54892993ADC0D1147221B";
Document doc1 = Jsoup.connect(request).userAgent("chrome").timeout(requestTime).get();
String data = doc1.toString();
String tempData = data.substring(data.indexOf("Related searches"));
tempData = tempData.substring(0, tempData.indexOf("</ul>"));
Document doc2 = Jsoup.parse(tempData);
Elements links = doc2.select("li");
ArrayList<String> keywordList = new ArrayList<String>();
for (Element link : links) {
String tempkeyword = link.text();
keywordList.add(tempkeyword);
}
for(int i = 0; i < keywordList.size(); i++) {
String tempKeyword = keywordList.get(i);
if(!suggestionsList.contains(tempKeyword)) {
suggestionsList.add(tempKeyword);
findSuggestions(tempKeyword, depth - 1);
}
}
} catch (Exception e) {}
}
};
new Thread(run).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment