1

我正在做一个项目,我希望他是隐形的。因此,我使用这个网站 - http://pytoexe.com/从我的 python 脚本创建一个基于窗口的 exe 文件,这意味着它不会使用 Windows 控制台。

不幸的是,因为我在我的代码中使用了 phantomjs 驱动程序,所以他打开了一个phantomjs 控制台,这打断了我。

为了解决这个问题,我需要添加一行或脚本来防止 phantomjs 控制台出现(更改我的 selenium 文件中的某些内容/类似的东西不起作用,因为它可能在他们的文件中出现问题,我无法做任何事情) .

有人知道该怎么做吗?

这是我的exe文件

这是我的代码:

from selenium import webdriver
import time
from PIL import Image
from constants import *
import operator
import os
#Email Constants
DEFAULT_CONTENT = 'example email stuff here'
HOST = 'smtp.gmail.com'
PORT = 587
EMAIL = 'freeadsandspam@gmail.com'
CUSTOMERS = []
SUBJECTS = ['new ad were found', 'ad were found by SMADS', 'best ad for you']
COMMASPACE = ', '
#Getting History
OUTPUT_FILE_PATH = 'C:\search_logger.txt'
COPY_OF_THE_HISTORY_PATH = 'C:\history'
NEW_OUTPUT_FILE_PATH = 'C:\last_search_logger.txt'
#PhantomJs And Ads Finding
PHANTOM_JS_PATH = 'C:\phantomjs-2.1.1-windows\\bin\phantomjs.exe'
OUTPUT_AD_PATH = 'ad.png'
DEFAULT_WINDOW_SIZE = (1024, 768)
AD_DATABASE = 'https://www.findads.com.au/'
KEYWORD_BUTTON_XPATH = '//*[@id="txtSearch"]'
SEARCH_BUTTON_XPATH = '/html/body/div[1]/div/form/button'
CONSTANT_TIME_SLEEPING = 3
AD_XPATH = '/html/body/div[1]/section/div/div[1]/div[4]/div[1]/div[1]/section[24]'
COMPARE_ELEMENT_XPATH = '//*[@id="fSearch"]'
CATAGORY_SORT_XPATH = '/html/body/div[1]/section/div/div[1]/div[5]/div/div[3]/form/div[1]/div[1]'



class PhantomJsDriver:
    """
    A class that taking care on the ads finding
    in the internet, doing it with PhantomJs -
    background driver
    """
    def __init__(self, ad_keyword, window_size=DEFAULT_WINDOW_SIZE, panthom_js_path=PHANTOM_JS_PATH, output_ad_path=OUTPUT_AD_PATH):
        """
        this function init our object
        in order to use the other functions
        that the object offer.

        Parameters
        ----------
        phantom_js_path : str
            path of the PhantomJs ( this essential because
            we cannot get the PhantomJs file otherwise)
        output_ad_path : str
            where you want to save the ad that the system
            had found and how you call the name of the ad
            file ( eg: ad.png )
        ad_keyword : str
            the keyword that define what ad the system bring
            ( eg: dog will bring dog ad )
        window_size : double int (int1,int2)
            define the window size of the browser ( mainly for the
            screenshot )
        """
        self.phantom_js_path = panthom_js_path
        self.output_ad_path = output_ad_path
        self.ad_keyword = ad_keyword
        self.window_size = window_size
        self.list_of_images = []
        self.dict = {}

    def get_ad(self):
        """
        this function save the ad by searching in the internet
        ( on specific website ) the keyword that the user chose
        and copy it into the output_ad_path.
        """
        for i in range(0, 5):
            driver = webdriver.PhantomJS(self.phantom_js_path)
            driver.set_window_size(self.window_size[0], self.window_size[1])
            driver.get(AD_DATABASE)
            keyword = driver.find_element_by_xpath(KEYWORD_BUTTON_XPATH)
            keyword.send_keys(self.ad_keyword)
            search_button = driver.find_element_by_xpath(SEARCH_BUTTON_XPATH)
            search_button.click()
            driver.save_screenshot("ad" + str(i) + ".png")
            element = driver.find_element_by_xpath(AD_XPATH)  # find part of the page you want image of
            self.crop_image(i, element)

    def crop_image(self, i, element):
        """
        this function crop the screenshot of the ads website from
        the previous function into one single ad.
        """
        im = Image.open("ad" + str(i) + ".png")  # uses PIL library to open image in memory
        location = element.location
        size = element.size
        left = location['x']
        top = location['y']
        right = location['x'] + size['width'] + 50
        bottom = location['y'] + size['height']
        weight, height = im.size
        print height
        im = im.crop((left, top, right, bottom))  # defines crop points
        im.save('test' + str(i) + '.png')  # saves new cropped image
        self.list_of_images.append('test' + str(i) + '.png')
        self.dict['test' + str(i) + '.png'] = 0

    def choose_the_best_ad(self):
        for img1 in self.list_of_images:
            for img2 in self.list_of_images:
                im1 = Image.open(img1)
                im2 = Image.open(img2)

                if list(im1.getdata()) == list(im2.getdata()):

                    self.dict[img1] += 1
                    self.dict[img2] += 1
        print self.dict
        BestImage = max(self.dict.iteritems(), key=operator.itemgetter(1))[0]
        print BestImage
        if os.path.exists("TheImage.png"):
            os.remove("TheImage.png")
        os.rename(BestImage, "TheImage.png")


driver = PhantomJsDriver("dog")
driver.get_ad()
driver.choose_the_best_ad()
4

0 回答 0