How would I log in on Facebook using a python programme? - python

I have a Python programme that I have stored my log in details on, How would I get the python programme to connect to the Facebook login page and input my log in details for me and then log in or return something?
E.g:
my details are in my main programme(Email and password), then I want to connect to Facebook, and have my program enter the details and send that off to Facebook.
Main Python File:
import urllib
import urllib2
def facebookDetails():
url = 'https://www.facebook.com/'
values = {'email' : 'somebody#facebook.com',
'pass' : 'password',
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

Here's an example using selenium:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
if __name__ == '__main__':
iuser = 'username'
ipass = 'password'
# You will also need to download PhantomJS
driver = webdriver.PhantomJS(pathToPhantomJS.exe)
driver.get('https://www.facebook.com/')
email = driver.find_element_by_xpath('//*[#id="email"]')
email.send_keys(iuser)
password = driver.find_element_by_xpath('//*[#id="pass"]')
password.send_keys(ipass)
login_button = driver.find_element_by_xpath('//*[#id="u_0_n"]')
login_button.click()

Related

How to edit a facebook post in Python?

I have a post on my fb page which I need to update several times a day with data elaborated in a python script. I tried using Selenium, but it gets often stuck when saving the post hence the script gets stuck too, so I'm trying to find a way to do the job within python itself without using a web browser.
I wonder is there a way to edit a FB post using a python library such as Facepy or similar?
I'm reading the graph API reference but there are no examples to learn from, but I guess first thing is to set up the login. On the facepy github page is written that
note that Facepy does not do authentication with Facebook; it only consumes its API. To get an access token to consume the API on behalf of a user, use a suitable OAuth library for your platform
I tried logging in with BeautifulSoup
from bs4 import BeautifulSoup
import requests
import re
def facebook_login(mail, pwd):
session = requests.Session()
r = session.get('https://www.facebook.com/', allow_redirects=False)
soup = BeautifulSoup(r.text)
action_url = soup.find('form', id='login_form')['action']
inputs = soup.find('form', id='login_form').findAll('input', {'type': ['hidden', 'submit']})
post_data = {input.get('name'): input.get('value') for input in inputs}
post_data['email'] = mail
post_data['pass'] = pwd.upper()
scripts = soup.findAll('script')
scripts_string = '/n/'.join([script.text for script in scripts])
datr_search = re.search('\["_js_datr","([^"]*)"', scripts_string, re.DOTALL)
if datr_search:
datr = datr_search.group(1)
cookies = {'_js_datr' : datr}
else:
return False
return session.post(action_url, data=post_data, cookies=cookies, allow_redirects=False)
facebook_login('email', 'psw')
but it gives this error
action_url = soup.find('form', id='login_form')['action']
TypeError: 'NoneType' object is not subscriptable
I also tried with Mechanize
import mechanize
username = 'email'
password = 'psw'
url = 'http://facebook.com/login'
print("opening browser")
br = mechanize.Browser()
print("opening url...please wait")
br.open(url)
print(br.title())
print("selecting form")
br.select_form(name='Login')
br['UserID'] = username
br['PassPhrase'] = password
print("submitting form"
br.submit()
response = br.submit()
pageSource = response.read()
but it gives an error too
mechanize._response.httperror_seek_wrapper: HTTP Error 403: b'request disallowed by robots.txt'
Install the facebook package
pip install facebook-sdk
then to update/edit a post on your page just run
import facebook
page_token = '...'
page_id = '...'
post_id = '...'
fb = facebook.GraphAPI(access_token = page_token, version="2.12")
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text')

Python : Download CSV file from a URL after login authentication

I want to download some data as a csv from an Airbnb that requires a login to download. So, I tried:
import urllib
import urllib.request
username = "username"
password = "password"
url = 'someurl'
values = { 'username': username,'password': password }
data = urllib.parse.urlencode(values).encode("utf-8")
response = urllib.request.urlopen(url, data)
result = response.read()
print (result)
I manage to get the API call URL through right click-> Inspect Element ->Network but I got
HTTP Error 403: Forbidden.
I tried using the requests module too:
import requests
username = "username"
password = "password"
url = 'someurl'
print(requests.get(url, auth=(username, password)).content)
But got an error:
b'{"error_code":401,"error":"authentication_required","error_message":"Please log in to continue."}'
Is there any way from which I can extract this data?
Edit: My Airbnb booking history's search query is the required URL here.

logging in to instagram with mechanize python

I have been trying to login to my instagram account with mechanize python for the past while and for some reason it is not working.
To check if I have logged in correctly, I decided to check the url with "br.geturl()", which should read "https://www.instagram.com/" once the login is successful, but after I run the program it is just:
"https://www.instagram.com/accounts/login/username=username_here&password=password_here"
Anyone know how to fix this?
Note: I know forsure my login info is correct.
Here is my code:
import mechanize
br = mechanize.Browser()
url = "https://www.instagram.com/accounts/login/"
br.set_handle_robots(False)
response = br.open(url)
f = list(br.forms())
br.form = f[0]
print br.form
br.form["username"] = 'username_goes_here'
br.form["password"] = 'password_goes_here'
br.submit()
print br.geturl()
Since the form is generated via JavaScript. So it could not be found on the html. One of the way would be using Selenium Webdriver.
from selenium import webdriver
driver = webdriver.Chrome('/Usr/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
username = driver.find_element_by_xpath('//*[#name="username"]')
password = driver.find_element_by_xpath('//*[#name="password"]')
login_btn = driver.find_element_by_xpath('//*[#class="oF4XW sqdOP L3NKy "]')
username.send_keys("username")
password.send_keys("password")
#test to see if input values are reflecting
print(username.get_attribute('value'))
print(password.get_attribute('value'))
#login
login_btn.click()
logged_in_class = driver.find_elements_by_class_name("logged-in")
not_logged_in_class = driver.find_elements_by_class_name("not-logged-in")
#to check if logged-in or not-logged-in
print(len(logged_in_class))
print(len(not_logged_in_class))
driver.quit()

How to login to a website with python and mechanize

i'm trying to log in to the website http://www.magickartenmarkt.de and do some analyzing in the member-area (https://www.magickartenmarkt.de/?mainPage=showWants). I saw other examples for this, but i don't get why my approaches didn't work. I identified the right forms for the first approach, but it's not clear if it worked.
In the second approach the returing webpage shows me that i don't have access to the member area.
I would by glad for any help.
import urllib2
import cookielib
import urllib
import requests
import mechanize
from mechanize._opener import urlopen
from mechanize._form import ParseResponse
USERNAME = 'Test'
PASSWORD = 'bla123'
URL = "http://www.magickartenmarkt.de"
# first approach
request = mechanize.Request(URL)
response = mechanize.urlopen(request)
forms = mechanize.ParseResponse(response, backwards_compat=False)
# I don't want to close?!
#response.close()
# Username and Password are stored in this form
form = forms[1]
form["username"] = USERNAME
form["userPassword"] = PASSWORD
#proof entering data has worked
user = form["username"] # a string, NOT a Control instance
print user
pw = form["userPassword"] # a string, NOT a Control instance
print pw
#is this the page where I will redirected after login?
print urlopen(form.click()).read ()
#second approach
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : USERNAME, 'userPassword': PASSWORD})
#login
response_web = opener.open(URL, login_data)
#did it work? for me not....
resp = opener.open('https://www.magickartenmarkt.de/?mainPage=showWants')
print resp.read()
Why not use a browser instance to facilitate navigation? Mechanize also has the ability to select particular forms (e.g. nr = 0 will select the first form on the page)
browser = mechanize.Browser()
browser.open(YOUR URL)
browser.select_form(nr = 0)
browser.form['username'] = USERNAME
browser.form['password'] = PASSWORD
browser.submit()
Web automation ? Definitely "WEBBOT"
webbot works even for webpages with dynamically changing id and classnames and has more methods and features than selenium.
Here's a snippet :)
from webbot import Browser
web = Browser()
web.go_to('google.com')
web.click('Sign in')
web.type('mymail#gmail.com' , into='Email')
web.click('NEXT' , tag='span')
web.type('mypassword' , into='Password' , id='passwordFieldId') # specific selection
web.click('NEXT' , tag='span') # you are logged in ^_^

logging into https site using python mechanize library

I have the following code:
import requests
import sys
import urllib2
import re
import mechanize
import cookielib
#import json
#import imp
#print(imp.find_module("requests"))
#print(requests.__file__)
EMAIL = "******"
PASSWORD = "*******"
URL = 'https://www.imleagues.com/Login.aspx'
address = "http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a"
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.add_password(URL, EMAIL, PASSWORD)
br.open(URL)
#br.open(URL)
#br.select_form(name="aspnetForm")
#br.form["ctl00$ContentPlaceHolder1$inUserName"] = EMAIL
#br.form["ctl00$ContentPlaceHolder1$inPassword"] = PASSWORD
#response = br.submit()
#br= mechanize.Browser()
site = br.open(address)
# Start a session so we can have persistant cookies
#session = requests.Session()
# This is the form data that the page sends when logging in
#login_data = {
# 'ctl00$ContentPlaceHolder1$inUserName': EMAIL,
# 'ctl00$ContentPlaceHolder1$inPassword': PASSWORD,
# 'aspnetFrom': 'http://www.imleagues.com/Members/Home.aspx',
#}
#URL_post = 'http://www.imleagues.com/Members/Home.aspx'
# Authenticate
#r = session.post(URL, data=login_data)
# Try accessing a page that requires you to be logged in
#r = session.get('http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a')
website = site.read()
f = open('crypt.txt', 'wb')
f.write(website)
#print(website_html)
I am trying to log into this site to monitor game times and make sure they aren't changed on me (again). I've tried various ways to do this, most commented out above, but all of them redirect me back to the login page. Any ideas? Thanks.
As I see in given website login button is not in submit tag. Login is javascript function
<a ... href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnLogin','')" </a>
and mechanize cannot handle javascript. I faced very similiar problem and came up with solution to use Spynner.
It is headless web browser. So you can acomplish same tasks as you use mechanize and it has javascript support.

Categories