Я хочу создать бота, который автоматически покупает предмет, если он доступен. Я хочу заполнить этот предмет, который я хочу приобрести, в GUI, а затем проверить, доступен ли он, и приобретает его, если так.
Я сделал GUI с Entry
:
from tkinter import *
import requests
import json
from selenium import webdriver
from time import sleep
root = Tk()
e = Entry(root, width=50)
e.pack()
def myClick():
myLabel = Label(root, text="Item Added")
myLabel.pack()
myButton = Button(root, text="Item Name", command=myClick)
myButton.pack()
root.mainloop()
Это часть GUI ^. Я хочу внедрить e.get
в скрипт бота (это работает, хотя):
def availabilitycheck():
r = requests.get('https://feature.com/products.json')
products = json.loads((r.text))['products']
for product in products:
#print(product['title'])
productname = product['title']
if productname == e.get:
producturl = 'https://feature.com/products/' + product['handle']
print('found Item')
return producturl
else:
return False
def buyProduct(url):
driver= webdriver.Chrome(executable_path=r'C:\Python38\Scripts\chromedriver.exe')
driver.get(str(url))
sleep(0.03)
#click size 10
driver.find_element_by_xpath('//div[@data-value="7"]').click()
sleep(0.265)
#click add to cart
driver.find_element_by_xpath('//button[@class="primary-btn add-to-cart"]').click()
sleep(1.70)
#click check out
driver.find_element_by_partial_link_text('Check Out').click()
sleep(0.50)
#enter email
driver.find_element_by_xpath('//input[@placeholder="Email"]').send_keys('email')
sleep(0.15)
#enter First name
driver.find_element_by_xpath('//input[@placeholder="First name"]').send_keys('my first name')
sleep(0.15)
#enter last name
driver.find_element_by_xpath('//input[@placeholder="Last name"]').send_keys('my last name')
sleep(0.15)
#enter addres +
driver.find_element_by_xpath('//input[@placeholder="Address"]').send_keys('my street name')
sleep(1.77)
#press enter-key for Google suggestion
driver.find_element_by_xpath('//input[@placeholder="Address"]').send_keys(u'\ue007')
sleep(0.15)
#enter phone + enter-key for to shipping
driver.find_element_by_xpath('//input[@data-backup="phone"]').send_keys('0612345678'+ u'\ue007')
sleep(1.98)
driver.find_element_by_xpath('//button[@class="step__footer__continue-btn btn"]').click()
sleep(1)
driver.find_element_by_xpath('//button[@class="step__footer__continue-btn btn"]').click()
sleep(1)
driver.find_element_by_xpath('//input[@id="number"]').send_keys('creditcard number')
while True:
myUrl=availabilitycheck()
if myUrl!=False:
buyProduct(myUrl)
break
else:
print('no')
Посмотрите на часть в начале:
if productname == e.get:
Когда я помещаю элемент в Entry
он ничего не делает, и когда я закрываю окно GUI, он просто отображает: нет, нет и т. д. c.
Вот весь сценарий:
from tkinter import *
import requests
import json
from selenium import webdriver
from time import sleep
root = Tk()
e = Entry(root, width=50)
e.pack()
def myClick():
myLabel = Label(root, text="Item Added")
myLabel.pack()
myButton = Button(root, text="Item Name", command=myClick)
myButton.pack()
root.mainloop()
def availabilitycheck():
r = requests.get('https://feature.com/products.json')
products = json.loads((r.text))['products']
for product in products:
#print(product['title'])
productname = product['title']
if productname == e.get:
producturl = 'https://feature.com/products/' + product['handle']
print('found Item')
return producturl
else:
return False
def buyProduct(url):
driver= webdriver.Chrome(executable_path=r'C:\Python38\Scripts\chromedriver.exe')
driver.get(str(url))
sleep(0.03)
#click size 10
driver.find_element_by_xpath('//div[@data-value="7"]').click()
sleep(0.265)
#click add to cart
driver.find_element_by_xpath('//button[@class="primary-btn add-to-cart"]').click()
sleep(1.70)
#click check out
driver.find_element_by_partial_link_text('Check Out').click()
sleep(0.50)
#enter email
driver.find_element_by_xpath('//input[@placeholder="Email"]').send_keys('email')
sleep(0.15)
#enter First name
driver.find_element_by_xpath('//input[@placeholder="First name"]').send_keys('first name')
sleep(0.15)
#enter last name
driver.find_element_by_xpath('//input[@placeholder="Last name"]').send_keys('last name')
sleep(0.15)
#enter addres +
driver.find_element_by_xpath('//input[@placeholder="Address"]').send_keys('street')
sleep(1.77)
#press enter-key for Google suggestion
driver.find_element_by_xpath('//input[@placeholder="Address"]').send_keys(u'\ue007')
sleep(0.15)
#enter phone + enter-key for to shipping
driver.find_element_by_xpath('//input[@data-backup="phone"]').send_keys('0612345678'+ u'\ue007')
sleep(1.98)
driver.find_element_by_xpath('//button[@class="step__footer__continue-btn btn"]').click()
sleep(1)
driver.find_element_by_xpath('//button[@class="step__footer__continue-btn btn"]').click()
sleep(1)
driver.find_element_by_xpath('//input[@id="number"]').send_keys('creditcard number')
while True:
myUrl=availabilitycheck()
if myUrl!=False:
buyProduct(myUrl)
break
else:
print('no')