1

How can i use selenium in my flask api i am trying to make an api which takes url as input and returns the title of that page but its not working

from selenium import webdriver
from flask import Flask


app = Flask(__name__)

@app.route("/try1/<string:url>")
def yt_downloader(url):
    path = r"D:\\Chromedriver.exe"
    driver = webdriver.Chrome(path)
    driver.get(url) 
    return(driver.title)
if __name__ == "__main__":
    app.run(debug=True)

This is the image of error

Spidy
  • 29
  • 7

1 Answers1

2

Since you are trying to pass an URL as parameter, you rather want to use a query parameter (e.g. try1?url=ADDRESS) or send the URL via a POST Request.

Using a Website URL and its' path within a path parameter is a pretty bad idea.

You can use a query parameter like this in a request:

from flask import request
url = request.args.get('url')
jpaodev
  • 136
  • 5
  • thanks for help but can you tell me where to write this line because i am not sure and code is throwing big errors also do i have to make any other changes in code? Thanks for help – Spidy Jul 21 '21 at 17:18
  • Hello. Sorry for the late reply, check the Flask documentation for further explanation, they offer great examples: https://flask.palletsprojects.com/en/2.0.x/quickstart/#accessing-request-data There's also a SO Post about getting the query parameter data: https://stackoverflow.com/questions/11774265/how-do-you-access-the-query-string-in-flask-routes - Here I created a snippet, which I tested, it's working fine for me: https://pastebin.com/DjDpxPAX – jpaodev Jul 21 '21 at 20:21