Forum in maintenance, we will back soon 🙂
Building First API
hey i followed what you taught in section "Build your first API > Build with AI", it given me url to open in brower. So you said at end of url add "/" and then keyword you want for auto suggestions, but in my case i did same nothing happened. I asked chatGpt about this case it said me this:
it just shown me
So why this happened? what is the solution?
@kunal-lonhare please share the code. I prefer you share it with GitHub but you can also use the code icon (<>) above the text entry.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
import requests from bs4 import BeautifulSoup from flask import Flask, request, jsonify app = Flask(__name__) # Function to fetch keyword suggestions from Google AutoSuggest def get_keyword_suggestions(keyword): base_url = "https://www.google.com/complete/search" params = { "q": keyword, "cp": "0", "client": "firefox", "hl": "en", } response = requests.get(base_url, params=params) if response.status_code == 200: suggestions = [] data = response.json()[1] for suggestion in data: suggestions.append(suggestion[0]) return suggestions else: return [] @app.route('/suggestions', methods=['GET']) def keyword_suggestions(): keyword = request.args.get('keyword') if not keyword: return jsonify({'error': 'Please provide a keyword parameter.'}), 400 suggestions = get_keyword_suggestions(keyword) return jsonify({'suggestions': suggestions}) if __name__ == '__main__': app.run(debug=True)
Your code is slightly different than mine. there is /suggestions in the URL, and then you need to pass the keyword as parameter.
try something like
.../suggestions?keyword=YOUR KEYWORD
@admin I'm going to be making a post on the differences soon; later today. I've coded both your example and the example ChatGPT gave me (and others).
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
from flask import Flask, request, jsonify import requests import json app = Flask(__name__) @app.route('/api/google-auto-suggestions', methods=['GET']) def google_auto_suggestions(): # Get the query parameter from the request query = request.args.get('query') # Make a request to the Google Autocomplete API url = "https://suggestqueries.google.com/complete/search?client=firefox&q={}".format(query) response = requests.get(url) # Parse the JSON response suggestions = json.loads(response.content) # Return the suggestions as a JSON response return jsonify(suggestions) if __name__ == '__main__': app.run()
import requests from bs4 import BeautifulSoup from flask import Flask, request, render_template, jsonify app = Flask(__name__) # Function to scrape Google auto-suggestions def get_google_suggestions(query): base_url = 'https://www.google.com/complete/search?q=' response = requests.get(base_url + query) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') suggestions = [s['data'] for s in soup.find_all('suggestion')] return suggestions return [] @app.route('/') def index(): return render_template('index.html') @app.route('/suggestions', methods=['POST']) def suggestions(): keyword = request.form.get('keyword') suggestions = get_google_suggestions(keyword) return jsonify({'suggestions': suggestions}) if __name__ == '__main__': app.run(debug=True)
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
@amr-atef isn't coding fun? So many ways to do the same thing. Your code from Blackbox has your URL as /api/google-auto-suggestions followed by ?query=KEYWORD.
The ChatGPT code you use /suggestions? keyword=KEYWORD in the URL.
HTH
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@admin sir it is showing like this, not actually a complete word, just one letter 🙁
Any solution to fix it up sir? I asked ChatGPT about this issue and that is what it is saying:
"I apologize for the confusion. The issue you're facing is likely due to the placeholder data used in the sample code. The code I provided in the original response does not actually fetch suggestions from Google's AutoSuggest service; it returns mock suggestions instead. To retrieve real suggestions from Google, you would need to use a legitimate Google API, but Google may have usage restrictions and require API keys.
For educational purposes, I provided a simple example. To fetch real suggestions from Google, you would need to implement a more complex solution that integrates with Google's API or use a third-party service that provides this functionality.
Please keep in mind that web scraping Google's search results may violate their terms of service, and they may block your IP address. It's essential to adhere to legal and ethical considerations when developing such applications."
@kunal-lonhare I think the problem is here:
for suggestion in data: suggestions.append(suggestion[0])
I would try removing the "[0]" so that it looks like:
for suggestion in data: suggestions.append(suggestion)
The reason I think this is because of something called "string indexing" which basically means all strings are an array of characters.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack