Created
March 1, 2015 05:59
-
-
Save onurvarol/d2f131b770587295a6cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys | |
import subprocess, signal | |
import time | |
from bs4 import BeautifulSoup | |
import selenium as sl | |
from selenium.webdriver import Firefox, FirefoxProfile | |
URL = 'https://www.usefulservice.com/' | |
profile = FirefoxProfile() | |
profile.set_preference('network.proxy.type', 1) | |
profile.set_preference('network.proxy.socks', '127.0.0.1') | |
profile.set_preference('network.proxy.socks_port', 9150) | |
driver = Firefox(profile) | |
def webpage_connect(imageUrl): | |
print 'Connecting...' | |
driver.get(URL) | |
driver.find_element_by_id("url_box").send_keys(imageUrl) | |
submitButton = driver.find_element_by_id('url_submit') | |
submitButton.click() | |
print imageUrl | |
trial = 0 | |
toParse = False | |
while trial < 100: | |
print 'Trying ...', trial | |
html = driver.page_source | |
if 'search-result-item-details' in html: | |
toParse = True | |
break | |
if "URL could not be reached" in html: | |
print 'Cant reached' | |
return ['url_unreacable'] | |
time.sleep(1) | |
trial += 1 | |
if trial >= 100: | |
return ['timeout'] | |
sources = list() | |
if toParse: | |
print 'parsing' | |
soup = BeautifulSoup(html) | |
for resultBox in soup.find_all('div', class_='search-result-item-details'): | |
res = resultBox.span.text.strip() | |
print res | |
sources.append(res) | |
return sources | |
def killProcess(processName='Firefox'): | |
#os.system("killall -9 {0}".format(processName)) | |
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE) | |
out, err = p.communicate() | |
for line in out.splitlines(): | |
if 'tor' in line.lower(): | |
continue | |
if processName.lower() in line.lower(): | |
pid = int(line.split(None, 1)[0]) | |
os.kill(pid, signal.SIGKILL) | |
if __name__ == '__main__': | |
testurl = 'http://onurvarol.com/testimage.png' | |
webpage_connect(testurl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment