-1

I am working on a program that scrapes recommended cafes from a review website and prints them based on the suburb inputted by the user. I am using an f-string on the URL to help the program fetch cafe lists from various suburbs depending on what is being inputted. Right now the program is asking for the User's Suburb but then not returning the cafe list, I can't figure out how to make the inputted suburb fill in the f-string...

def get_cafes(user_suburb):
#open url
    url_cafes = f"https://www.broadsheet.com.au/melbourne/guides/best-cafes-{user_suburb}"
    html_cafes = urlopen(url_cafes)

#create beautiful soup object for cafes
    soup_cafe_list = BeautifulSoup(html_cafes, 'html.parser')
    type(soup_cafe_list)

#grab cafe names
    cafe_names = soup_cafe_list.find_all("h2", class_= "venue-title")
    print (cafe_names)

#function to search cafes        
    def cafe_search():
        user_suburb = input("What Suburb?")
        if user_suburb in suburbs_lower:
            print(get_cafes("user_suburb"))

The most important part, I believe, is in the def cafe_search() function. NB The "suburbs_lower" variable refers to some code that appears above the quoted code.

Any ideas how I can get the inputted suburb to fill in the f-string?

Thanks in advance!

deadant88
  • 920
  • 9
  • 24

1 Answers1

1

I suspect the error is in the function call. To test, try this:

def get_cafes(user_suburb):
    url_cafes = f"https://www.broadsheet.com.au/melbourne/guides/best-cafes-{user_suburb}"
    print(url_cafes)

user_suburb = input("What Suburb?")
if user_suburb in suburbs_lower:
    get_cafes(user_suburb) #note: no quote marks

It should output a correctly formed url.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45