How to fix MissingSchema error in Python requests?

MissingSchema occurs when you don't provide the complete URL to requests. This often means you skipped http:// or https:// and/or provided a relative URL.

You can fix this error by making use of the urljoin function from the urllib.parse library to join URLs before making a remote request. The solution will look something like this:

from urllib.parse import urljoin
import requests

url = ""
relative_url = "/path/to/resource"

final_url = urljoin(url, relative_url)
html = requests.get(final_url)

urljoin will merge two URLs only if the second argument is a relative path. For example, the following sample code will print

from urllib.parse import urljoin

first_url = ""
second_url = ""

final_url = urljoin(first_url, second_url)

Related Requests web scraping questions: