Skip to content

Forum in maintenance, we will back soon 🙂

Building First API
 
Notifications
Clear all

Building First API

10 Posts
4 Users
2 Reactions
578 Views
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
Topic starter
 

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?

 
Posted : 10/15/2023 11:11 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 10/15/2023 2:05 pm
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
Topic starter
 

@ssadvisor

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)
 
Posted : 10/15/2023 2:07 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

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

 
Posted : 10/15/2023 3:00 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 10/15/2023 3:45 pm
(@amr-atef)
Posts: 15
Eminent Member Customer
 
I also got the same problem, I tried your solution Hasan by writing "/suggestions? Keyword=YOUR KEYWORD" but also it didn't work
 
 
 
the first option is from Blackbox AI Code Chat

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()
 
second one is from the ChatGPT:

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 erorr I got is:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
 
Posted : 10/15/2023 5:12 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 10/15/2023 8:02 pm
Amr Atef reacted
(@amr-atef)
Posts: 15
Eminent Member Customer
 

@SSAdvisor yeah too much fun actually 😀

 
Posted : 10/15/2023 9:26 pm
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
Topic starter
 

@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."

This post was modified 1 year ago by Kunal Lonhare
 
Posted : 10/16/2023 4:08 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 10/16/2023 2:17 pm
Share:
[the_ad_group id="312"]