get number of google search results - java

I searched a lot to retrieve the number of search results in google using java, but nothing worked.
I have tried Google Custom Search API aswell.
I don't want the title/url of results, just number of total results found.
Can some one please guide me?

By using the Custom Search API, you're on the right way.
There's a totalResults key in the response JSON that you get from your query. Just grab it's value and you're done.
If you want your JSON to only contain that value, add the fields parameter to your query like that:
https://www.googleapis.com/customsearch/v1?key={YOUR_API_KEY}&cx={YOUR_SEARCH_ENGINE_ID}
&q={YOUR_SEARCH_STRING}&alt=json&fields=queries(request(totalResults))

Related

Java - use searchbar on given website

Let me just start by saying that this is a soft question.
I am rather new to application development, and thus why I'm asking a question without presenting you with any actual code. I know the basics of Java coding, and I was wondering if anyone could enlighten me on the following topic:
Say I have an external website, Craigslist, or some other site that allows me to search through products/services/results manually by typing a query into a searchbox somewhere on the page. The trouble is, that there is no API for this site for me to use.
However I do know that http://sfbay.craigslist.org/search/sss?query=QUERYHERE&sort=rel points me to a list of results, where QUERYHERE is replaced by what I'm looking for.
What I'm wondering here is: is it possible to store these results in an Array (or List or some form of Collection) in Java?
Is there perhaps some library or external tool that can allow me to specify a query to search for, have it paste it in to a search-link, perform the search, and fill an Array with the results?
Or is what I am describing impossible without an API?
This depends, if the query website accepts returning the result as XML or JSON (usually with a .xml or .json at the end of url) you can parse it easily with DOM for XML on Java or download and use the JSONLibrary to parse a JSON.
Otherwise you will receive a HTML that is the page that a user would see in a browser, then you can try parse it as a XML but you will have a lot of work to map all fields in the HTML to get the list as you want.

How to parse result by Custom Search Engine

I'm using Jsoup to parse data from a website. But I don't know how to parse search result by CSE (custom search engine).
Please review images below.
Search Result
In search result, I want to get: image, title, link and description.
If you know, you can give me some solution!
Link Search:
http://www.truyenngan.com.vn/tim-kiem.html?q=love&cx=000993172113723111222%3Auprumhk-rde&cof=FORID%3A11&ie=UTF-8&siteurl=www.truyenngan.com.vn%2F&ref=&ss=419j62441j4
`
When you'll parse page.asXml() you will get the source code which will definitely fetch the whole page data.
You need to apply some parsing logic,as the links will be with particular div/class/id ,so you can fetch them by looping.
Document doc = Jsoup.parse(page.asXml());
Elements elements = doc.getElementsByTag('<id/div/class>');
Iterate elements to get value of all the links and description.
Use the Custom Search Engines API, and you will be getting parsed results in JSON.

filtering tweets based on two condition twitter API

I have filtered tweets based on location using
q.location(bbox);
If i have further check whether these tweets have set of keywords I used
q.track("game")
My tweets either have the location or the words not both the conditions are met.
Are my statements correct? Where did I go wrong?
Unfortunately, if you are using the streaming api, this is the expected behaviour - from the documentation (emphasis mine):
The track, follow, and locations fields should be considered to be combined with an OR operator. track=foo&follow=1234 returns Tweets matching "foo" OR created by user 1234.
So in other words, you should get Tweets with the bbox location or containing the keyword game.
If you want to keep on using the streaming api, a possible solution may be to manually check received Tweets for the other condition, i.e. if the Tweet had the correct location then check the status text for the keyword etc...
Otherwise, consider using the search api instead. See Twitter#search(Query), for example:
final Query query = new Query("game");
query.setGeocode(...);
final QueryResult result = twitter.search(query);
You should find that Twitter#search(Query) works more like the search on Twitter's website.
However, be aware that by using the search api, you may miss some Tweets:
...it's important to know that the Search API is focused on relevance and not completeness. This means that some Tweets and users may be missing from search results. If you want to match for completeness you should consider using a Streaming API instead.
(source)
I don't know the twitter4j API, but the twitter API allows this, you can test it with the twitter advanced search test page.
Are you able to capture the get request that is issued?
Are you using Streaming API in twitter4J? If so, I can recommend you to look at an example below:
FilterQuery fq = new FilterQuery();
String keywords[] = {"health", "politics"};
double[][] loc={{-122.75,36.8},{-121.75,37.8}};
fq.locations(loc);
fq.track(keywords);
twitterStream.addListener(statusListener);
twitterStream.filter(fq);

Remove query parameters that Google appends to search results

Thanks for taking the time to answer my question.
I have written a function in Java which connects to google.com and collects the first 200 search results returned given a query. However, Google appends some "funky" parameters to the original url so the original:
http://en.wikipedia.org/wiki/World_Chess_Championship_2013
becomes:
http://en.wikipedia.org/wiki/World_Chess_Championship_2013&sa=U&ei=EPiIUuSGB5OV7AbB0YCQCA&ved=0CCMQFjAD&usg=AFQjCNEsQZZJUO1CU7cCwBaUDAXP9LSsjQ.
Now this would not be a problem since I could just cut the String at the point where I encounter "&sa..". However, Google appends different parameters for different data types. So a PDF link contains one set of parameters, images another, websites a third one etc..
Do you know of a way where I can programatically remove the parameters that google appends in order to get the original url?
Thanks

querying multiple results from MediaWiki / Wikipedia using Android or Java

I am currently using MediaWiki's URL example to query HTTP GET requests on android.
I am simply getting information through a URL like this;
http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=Main%20Page&prop=revisions&rvprop=content
However, in this example, I always need some sort of direct title and only get one result back (titles=some name here)
I know that Wikipedia has more complex search methods explained here;
http://en.wikipedia.org/wiki/Help:Searching
I would like to offer a few "previews" of multiple wikipedia article per search, since what they type might not always be what they want.
Is there any way to query these special "search" results?
Any help would be appreciated.
It looks like the MediaWiki search API may be what you're after. That particular page discusses getting previews of search results.

Categories