Nathan
Nathan

Reputation: 13

Python - A Requests library newbie

this is my first post on those forums and hopefully you'll understand that, i am a programming newbie as well. So, i want to do my first project in Python using the requests library about something that i'm quite passionate about. It's an osu! map downloader in which you will be able to download maps through a command line. First of all, i am learning the requests library to get access to the website (login). This is my code so far:

import requests
import time

inUserName = input("Nickname: ")
inUserPass = input("Password: ")
req = requests.get("http://osu.ppy.sh/p/api")

from requests.auth import HTTPBasicAuth
requests.get("https://osu.ppy.sh/forum/ucp.php?mode=login", auth=HTTPBasicAuth(inUserName, inUserPass))

with requests.Session() as session:
       POSTrequest = session.post(url=req, data={'username': inUserName, 'password': inUserPass})

time.sleep(2)

I'm geting Response 200 from this, which is great.. but when a few months ago i've used a similar code i got some HTML which wasn't helpful either, i knew that i wasn't getting connected in any way.

The error is:

requests.exceptions.MissingSchema: Invalid URL '<Response [200]>': No schema supplied. Perhaps you meant http://<Response [200]>?

Any help in improving the code would be appreciated, thanks very much and sorry for any English mistakes i've made, not the first language.

Upvotes: 0

Views: 217

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28807

So you've specified the url for one of your posts improperly.

import requests
import time

inUserName = input("Nickname: ")
inUserPass = input("Password: ")
osuApiUrl = "http://osu.ppy.sh/p/api"
session = requests.Session()
req = session.get(osuApiUrl)


session.get("https://osu.ppy.sh/forum/ucp.php?mode=login", auth=(inUserName, inUserPass))

POSTrequest = session.post(url=osuApiUrl, data={'username': inUserName, 'password': inUserPass})

if POSTrequests.status_code == 200:
    print(POSTrequests.text)
    print(r.status_code)
time.sleep(2)

Further, if any cookies are set by the server, you should be using a session for the entire script. By using the functional API, you're creating and discarding a session for each request. This is much more efficient.

Upvotes: 1

Related Questions