Using Java to automate google and print output - java

Does anyone know if there's an easy way to implement this:
I have an array of strings (~650 elements) and would like to google each element, and then have the number of results acquired in each search stored in another integer array. I looked at the Google API, but it seems a little daunting to me, it seems like a fairly simple task, but can't seem to find any guidance on how to do this.
Help is much appreciated.
Thanks.

My first thought was to do the following. I don't think its the fastest or most elegant way, but I think it should do the trick:
If you check a URL on Google, they all look the same:
http://www.google.com/search?q='here your searchword'
So i would just iterate over your array and for each string create a url like e.g.
http://www.google.com/search?q=test
do a HTTP-Post for that and parse the result.
Here is a short example of the HTTPPost. You can also see how to get the response. I don't know how googles response is looking, but if you just print out the whole response, I think you will see how you have to parse it.

Related

Get TEXT only tweets using Twitter4j ? tweet that do not contain any media

There are a lot of forums asking to get medias from twitter but how to do the opposite? I want to get only the tweets which do not contain any image/video/url. If found those tweets just skip and search for the next because I want to display a full text without "http://t..." thing at the end. I put this in ...
cb.setIncludeEntitiesEnabled(false);
but, was not sure I did it right. Also, I write this code in Processing library but in Eclipse so I guess if you can show me the way in Java I will be fine, but a complete example please. I am very new to Java.
However, I have seen some people say about "filter=image" in tweet method but, I could not figure it out where to put this in. I have tried and fail.
Any suggestion? --Thanks in advance.
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. Please try to be more specific. Saying "I could not figure it out where to put this in" doesn't really tell us anything. Where exactly did you try to put it? What exactly happened when you tried that? Can you please post a MCVE?
That being said, the best thing you can do is Google something like "twitter4j filter out images" or "twitter4j exclude images". More info:
Get Tweets with Pictures using twitter search api: Notice the second reply mentions the approach of getting all tweets and then manually checking whether it has an image. You could do the same thing to check that a tweet does not contain an image.
How to exclude retweets from my search query results Options: This mentions an exclude filter and a -filter that might be helpful.
Please try something and post a MCVE in a new question post if you get stuck. Good luck.

Building JSON url String

This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.

How to get the Google's search result using Java

According to the answer in here, using Gson we can programmatically achieve to retrieve the result that Google will return to a query. Nonetheless, yet there are 2 questions are remaining in my mind:
How can we do similar thing for Bing?
How can we get more than 4 results based on the referred answer? Because the results.getResponseData().getResults().get(n).getUrl() for n>4 returns exception.
As #Niklas noted, google search api is deprecated, thus you should not use it for your project. Currently the only solution would be to get search result by http request to get a html search results and than parse it yourself.
In case of Bing, there is a search API, but it has a limited number of calls for free users. If you need to make a lot of requests, than you will have to pay for it. https://datamarket.azure.com/dataset/5BA839F1-12CE-4CCE-BF57-A49D98D29A44

RESTful webservice with unknown number of parameters

I am writing a RESTful web service using Java which should be able to take as (POST) input an unknown number of parameter name, value pairs. I am thinking what is the best way to achieve this.
The POST input will be JSON with the expected format being:
{"input" :
{ "x" = 1, "y"="Hello, "z"="1.2.3.4",... }
}
The parameter values are expected to be a mix of int/long/float/string etc. The expected behavior is to save this input to the DB and provide it back on request. The tricky part is with the parameter names being unknown, I cannot write getter and setter methods. Is there a simple way for handling this?
Any pointers in the right direction will be very much appreciated. Can I read this as a List? Do I need to write Custom MessageBodyReaders? Is there some other way of achieving this? The only option I can think of is to have these sent as a file and parse it but am hoping that there are simpler/cleaner solutions. Thank you for any pointers...
If all you need to do is save it and make it available on request, why parse it at all? Simply write the json as a string to your database, and return it as a string while setting the response type to "application/json". The one caveat being you're going to have to have some way to identify which json string you want and associate a unique id that you're going to be able to determine when you're requesting the specific json string in the future.
Something worth noting here would be that when you don't parse something, you don't actually know what it is. Even though a lot of modern libraries/frameworks sanitize against sql injection/xss already, it would be worth it to verify that this is being done in your server if you don't already know for sure.
That being said, if you're still feeling as though you need to parse the json, I would look into gson: http://code.google.com/p/google-gson/

Create a method to get integers from a https web page and store it in a array

Can someone please help me create a method to get the integers (listed below) from this link and store them in an array using Java? I'm new to Java and I've searched around and I can't any info or working examples of how to do it.
The integers are:
17,04,2011,1,2,7,10,13,23,24,25,26,27,38,39,41,43,45,48,49,55,59,62
All integers will change each time the method is called.
You need to do this:
retrieve the web page. Use URL and URLConnection for this.
Parse the HTML. It looks like this is an XHTML page, so you could use any XML API for this. As an alternative, for this simple structure something like a regular expression that throws away anything which is in <...> could work, too.
put the numbers from the HTML into a new array. You might want to count them first, to know the right size, or put them in while you are parsing, and later cut off the array. (Or they are always the same number.)

Categories

Resources