Skip to content

Instantly share code, notes, and snippets.

@nikolak
Last active November 17, 2021 22:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikolak/f9bee3497d12286eded2 to your computer and use it in GitHub Desktop.
Save nikolak/f9bee3497d12286eded2 to your computer and use it in GitHub Desktop.
Basic login to craigslist using python
import requests
# Some headers may bbe omitted, but IMO it's best to keep these:
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
"Host": "accounts.craigslist.org",
"Origin": "https://accounts.craigslist.org",
"Referer": "https://accounts.craigslist.org/login"}
# form POST data
data = {"step": "confirmation",
"inputEmailHandle": "user@example.com",
"inputPassword": "password"}
# Session object to keep cookies and stuff
s = requests.Session()
# First just GET the login page to get the cookies and other
# stuff that's often unique for a session
s.get("https://accounts.craigslist.org/login")
# Make a POST request with headers and form data from above that
# will log us in
r = s.post('https://accounts.craigslist.org/login',
headers=headers, data=data)
# We should now be logged in, GET the home page
r = s.get("https://accounts.craigslist.org/login/home")
print r.text # Shows HTML of the home page
@roccocheng
Copy link

Hi, I was able to use this code to log into my craigslist account without any issues. But when I was trying to use for other website logins (like pge, bank accounts), I couldn't make it work. I did make all the necessary changes without modifying the template of your code. Can you please give me some general ideas about what might have gone wrong? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment