Problems with loadStrings(url)
in
Programming Questions
•
2 years ago
Hi
I have a problem with a sketch I'm working on. It's a program that will take a word from a .csv file, then use that word to search for pictures on Google and retrieve the resulting html file for parsing.
The code I have so far is this. It's been modified slightly for your viewing pleasure.
- String[] google_csv;
- String[] result_line;
- String[] result_words;
- String[] search_result;
- String[] html;
- String final_result = "";
- String url;
- void setup() {
- //html = parse_html(parse_csv()); //This is the normal call
- html = parse_html("searchword"); //This call will work without the .csv file
- }
- //Nothing in the draw function for now---------------------------------------------------------------------
- void draw() {
- }
- //The function to parse the .csv file----------------------------------------------------------------------
- String parse_csv() {
- google_csv = loadStrings("report.csv");
- result_line = split(google_csv[60], ',');
- result_words = split(result_line[0], ' ');
- for(int i = 0; i < result_words.length; i++) {
- final_result += result_words[i];
- if(i < (result_words.length - 1)) {
- final_result += "+";
- }
- }
- return final_result;
- }
- //The function to fetch and parse the html file with the search results-----------------------------------
- String[] parse_html(String word) {
- url = "http://images.google.com/images?hl=da&biw=1166&bih=706&tbs=isch%3A1%2Cisz%3Al%2Citp%3Aphoto&sa=1&q=%22" + word + "%22&aq=f&aqi=g10&aql=&oq=";
- search_result = loadStrings(url); //This is where it fails to find the URL
- return search_result;
- }
java.io.IOException: Server returned HTTP response code: 403 for URL: http://images.google.com/images?hl=da&biw=1166&bih=706&tbs=isch%3A1%2Cisz%3Al%2Citp%3Aphoto&sa=1&q=%22searchword%22&aq=f&aqi=g10&aql=&oq=
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.URL.openStream(URL.java:1010)
at processing.core.PApplet.createInputRaw(PApplet.java:4532)
at processing.core.PApplet.createInput(PApplet.java:4500)
at processing.core.PApplet.loadStrings(PApplet.java:4785)
at sketch_mar31a.parse_html(sketch_mar31a.java:62)
at sketch_mar31a.setup(sketch_mar31a.java:32)
at processing.core.PApplet.handleDraw(PApplet.java:1583)
at processing.core.PApplet.run(PApplet.java:1503)
at java.lang.Thread.run(Thread.java:680)
The file "http://images.google.com/images?hl=da&biw=1166&bih=706&tbs=isch%3A1%2Cisz%3Al%2Citp%3Aphoto&sa=1&q=%22searchword%22&aq=f&aqi=g10&aql=&oq=" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
I know the url is valid, because if I cut the url from the error report and paste it in the browser it works.
What gives? Have I missed something obvious? (I'm kinda new to this)
/Morten
1