From fff43f6410d4cba7f4840e32b054dc1a065674d8 Mon Sep 17 00:00:00 2001 From: Dibya Ranjan Jena Date: Sun, 22 Oct 2017 06:51:05 +0530 Subject: [PATCH 1/4] Fb.py fixed Scraping actual Facebook site is a little problematic as getting its relative xpath is difficult. So we are using (https://mbasic.facebook.com/) which has simple HTML format and loads up quickly. Only thing is that after login the URL changes, so redirecting it again to previous URL is required or else it throws element not found error. It's now complete --- fb.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/fb.py b/fb.py index ce3d111..7f0cae1 100644 --- a/fb.py +++ b/fb.py @@ -1,10 +1,11 @@ import time +import platform import getpass import selenium from selenium import webdriver from bs4 import BeautifulSoup from selenium.webdriver.common.keys import Keys -from selenium.webdriver.firefox.firefox_binary import FirefoxBinary +from selenium.webdriver.phantomjs.webdriver import WebDriver def get_title(driver): @@ -13,9 +14,12 @@ def get_title(driver): print(soup.title.string+"\n") return soup.title.string -binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe') -driver = webdriver.Firefox(firefox_binary=binary) -url = "https://facebook.com" +if platform.system() == 'Windows': + pathr = 'C:\\Program Files\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe' +else: + pathr = './phantomjs' +driver = webdriver.PhantomJS(pathr, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) +url = "https://mbasic.facebook.com/" while True: driver.get(url) @@ -30,19 +34,21 @@ def get_title(driver): password = driver.find_element_by_name("pass") password.send_keys(pAss) - login = driver.find_element_by_css_selector("#u_0_l") + login = driver.find_element_by_name("login") login.click() time.sleep(5) + if get_title(driver) != "Log in to Facebook | Facebook": print("You are logged in now\n") + #afterlogin the url changes so we need to set the url again + driver.get(url) pp = input("Enter your Status\n") - # status = driver.find_element_by_xpath('//textarea[@class="uiTextareaAutogrow _3en1" or @class="uiTextareaAutogrow _3en1 _480e"]') - status = driver.find_element_by_xpath('//textarea[contains(@class, "uiTextareaAutogrow")]') + status = driver.find_element_by_name("xc_message") status.send_keys(pp) - button = driver.find_element_by_xpath('//div[@class="clearfix"]/div[contains(@class, "rfloat")]/div/button[@type="submit" and @value="1"]') + button = driver.find_element_by_name("view_post") button.click() time.sleep(5) print("Your Status : '"+pp+"' has been uploaded!\n") @@ -50,4 +56,4 @@ def get_title(driver): break else: - print("Invalid Email Id or Password, Try Login Again\nCtrl+C to exit the script\n") \ No newline at end of file + print("Invalid Email Id or Password, Try Login Again\nCtrl+C to exit the script\n") From 467f1fb265f74e4892a9725c2951c46edc22b0e9 Mon Sep 17 00:00:00 2001 From: Subhrajit Prusty Date: Sun, 22 Oct 2017 12:01:24 +0530 Subject: [PATCH 2/4] changed some stuff for optimisation, fixed some errors, changed README --- .gitignore | 3 +++ README.md | 28 +++++++++++++--------------- fb.py | 14 +++++++------- 3 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..681f634 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +ENV +ghostdriver.log +phantomjs.exe diff --git a/README.md b/README.md index bb9708c..45fcee7 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,28 @@ # FbPostUsingPython ->It is a small script to post status on your FB wall using python..! +>It is a small script to post status on your FB wall using python -#Installation -1- Install python 3.5 from [Python.org](https://www.python.org) +# Installation +* Install python 3 from [Python.org](https://www.python.org) -2- `sudo apt-get install python3-pip` (For Ubuntu Users) +* Install pip `$ sudo apt-get install python3-pip` (For Ubuntu Users) -3- `python setup.py` or `python3 setup.py` +* `$ python setup.py` or `$ python3 setup.py` - This will install all these packages +* Download [PhantonJS](http://phantomjs.org/download.html) and copy the exe file into the same folder as your repository - * `pip install bs4` +After the installation, open terminal at the root folder - * `pip install selenium` +Run `python fb.py` or `python3 fb.py` to post status from terminal or cmd. -After the installation, open terminal at the root folder-- +Note:- Use Mozilla Firefox V40.0+ to make this script work for you. -Run `python fb.py` or `python3 fb.py` to post status from terminal or cmd. -Note:- Use Mozilla Firefox V40.0 to make this script work for you. -##Some Useful Installation Tips ( Ubuntu ) +## Some Useful Installation Tips ( Ubuntu ) -If you have already installed python 2.7 install python 3 as well but it may be the problem that the packages installed with respect to python 2.7 and shows error for the python 3 packages, +If you have already installed python 2.7 install python3 as well but it may be the problem that the packages are installed with respect to python 2.7 and shows error for the python 3 packages, -So you need to install virtual Environment for the python 3 to install python3 packages. +So, you need to install a Virtual Environment for the python3 to install python3 packages. ``` virtualenv -p /usr/bin/python3 py3env @@ -34,7 +32,7 @@ So you need to install virtual Environment for the python 3 to install python3 p After setting virtual environment install packages listed above and Enjoy. -#License +# License Copyright (c) 2016 Ankit Jain - Released under the Apache License diff --git a/fb.py b/fb.py index 7f0cae1..8ab69fb 100644 --- a/fb.py +++ b/fb.py @@ -1,5 +1,5 @@ import time -import platform +import os import getpass import selenium from selenium import webdriver @@ -11,11 +11,11 @@ def get_title(driver): html_page = driver.page_source soup = BeautifulSoup(html_page,'html.parser') - print(soup.title.string+"\n") + # print(soup.title.string+"\n") return soup.title.string -if platform.system() == 'Windows': - pathr = 'C:\\Program Files\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe' +if os.name == 'nt': + pathr = 'phantomjs.exe' else: pathr = './phantomjs' driver = webdriver.PhantomJS(pathr, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) @@ -23,7 +23,7 @@ def get_title(driver): while True: driver.get(url) - time.sleep(2) + # time.sleep(2) get_title(driver) eMail = input("Enter your Email Id or Mobile No. to Login\n") @@ -36,7 +36,7 @@ def get_title(driver): login = driver.find_element_by_name("login") login.click() - time.sleep(5) + # time.sleep(5) if get_title(driver) != "Log in to Facebook | Facebook": @@ -50,7 +50,7 @@ def get_title(driver): status.send_keys(pp) button = driver.find_element_by_name("view_post") button.click() - time.sleep(5) + # time.sleep(5) print("Your Status : '"+pp+"' has been uploaded!\n") driver.quit() break From b4a0bb6db93c6f864fe20fc48857474ba429364b Mon Sep 17 00:00:00 2001 From: Subhrajit Prusty Date: Sun, 22 Oct 2017 12:03:44 +0530 Subject: [PATCH 3/4] removed Mozilla from readme, fixed spellings --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45fcee7..72bad9b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ * `$ python setup.py` or `$ python3 setup.py` -* Download [PhantonJS](http://phantomjs.org/download.html) and copy the exe file into the same folder as your repository +* Download [PhantomJS](http://phantomjs.org/download.html) and copy the exe file into the same folder as your repository After the installation, open terminal at the root folder From 98497260576bff65ad2876b6c26d7b7e19bc4974 Mon Sep 17 00:00:00 2001 From: Subhrajit Prusty Date: Sun, 22 Oct 2017 12:09:52 +0530 Subject: [PATCH 4/4] bleh --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 72bad9b..f9b7994 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,6 @@ After the installation, open terminal at the root folder Run `python fb.py` or `python3 fb.py` to post status from terminal or cmd. -Note:- Use Mozilla Firefox V40.0+ to make this script work for you. - ## Some Useful Installation Tips ( Ubuntu )