Как применить многопроцессорность к этому сценарию Python3? - PullRequest
0 голосов
/ 26 мая 2020

Я работал над этим кодом с намерением использовать его для покупок в Интернете. Как видите, код должен открывать страницу и вводить прокси, затем ждать, пока продукт не упадет, и покупать его. У меня было много проблем с поиском способа интегрировать способ запуска этого кода несколько раз одновременно, чтобы я мог много раз проверять, когда продукт падает. Я прочитал бесчисленное количество часов о многопроцессорности, но не могу понять, как применить это к моему коду. Может ли кто-нибудь помочь мне, исправив мой код? Предупреждающее спасибо за уделенное время :)

import time
from threading import Thread
import pyautogui
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from lxml.html import fromstring
import requests
from itertools import cycle
import traceback
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t
from datetime import datetime, time
from time import sleep

hostname = "hostname"
port = "378798"
proxy_username = "kajflnsdlkfjds"
proxy_password = "kjdnfakljdsn"

chrome_options = Options()
chrome_options.add_argument('--proxy-server={}'.format(hostname + ":" + port))
driver = webdriver.Chrome(options=chrome_options)

def enter_proxy_auth(proxy_username, proxy_password):
    t.sleep(2)
    pyautogui.typewrite(proxy_username)
    pyautogui.press('tab')
    pyautogui.typewrite(proxy_password)
    pyautogui.press('enter')


def open_a_page(driver, url):
    driver.get(url)


Thread(target=open_a_page, args=(driver, "http://www.google.com/")).start()
Thread(target=enter_proxy_auth, args=(proxy_username, proxy_password)).start()

def act(x):
    return x+10

def wait_start(runTime, action):
    startTime = time(*(map(int, runTime.split(':'))))
    while startTime > datetime.today().time(): # you can add here any additional variable to break loop if necessary
        sleep(1)# you can change 1 sec interval to any other
    return action
wait_start('21:51', lambda: act(100))
driver.get('https://url')

product = '//*[@id=""]/button/span'

price = '//*[@id=""]'

check_out_now = '//*[@id=""]/div/div[1]/button'

name = '//*[@id=""]/div[1]/div[2]/div/form/div[2]/label/input'

street = '//*[@id=""]/div[1]/div[2]/div/form/div[4]/div[1]/label/input'

town = '//*[@id=""]/div[1]/div[2]/div/form/div[4]/div[3]/label/input'

state_first = '//*[@id=""]/div[1]/div[2]/div/form/div[4]/div[4]/label/select/option[37]'

zipcode = '//*[@id=""]/div[1]/div[2]/div/form/div[4]/div[5]/label/input'

continue_to_payment = '//*[@id=""]/div[1]/div[2]/div/form/button'

proceed_to_paypal = '//*[@id=""]/div[2]/div[2]/div[3]/button'

pay_with_debit = '//*[@id=""]'

card_number = '//*[@id=""]'

exp_info = '//*[@id=""]'

cvv = '//*[@id=""]'

first_name = '//*[@id=""]'

last_name = '//*[@id=""]'

address = '//*[@id=""]'

city = '//*[@id=""]'

state = '//*[@id=""]/div/div/div[4]/div/div/span/span/span'

zip_code = '//*[@id=""]'

phone = '//*[@id=""]'

email = '//*[@id=""]'

pay_now = '//*[@id=""]'



driver.implicitly_wait(30)
  #driver.find_element_by_xpath(pay_now).click()
driver.find_element_by_xpath("//span[contains(.,'')]").click()
#driver.find_element_by_xpath(price).send_keys("")
driver.find_element_by_xpath(check_out_now).click()
driver.find_element_by_xpath(name).send_keys("")
driver.find_element_by_xpath(street).send_keys("")
driver.find_element_by_xpath(town).send_keys("")
driver.find_element_by_xpath(zipcode).send_keys("")
driver.find_element_by_xpath(state_first).click()
driver.find_element_by_xpath(continue_to_payment).click()
driver.find_element_by_xpath(proceed_to_paypal).click()
driver.find_element_by_xpath(pay_with_debit).click()
driver.implicitly_wait(10)
driver.find_element_by_xpath(exp_info).send_keys("")
el = driver.find_element_by_xpath(card_number)
for character in "":
    el.send_keys(character)
    t.sleep(0.0075)
driver.find_element_by_xpath(cvv).send_keys("")
driver.find_element_by_xpath(phone).send_keys("")
driver.find_element_by_xpath(email).send_keys("johndoe@gmai.com")
driver.find_element_by_xpath(pay_now).click()
...