问题
I am trying to print to write to a file what type of shipping and item has
from bs4 import BeautifulSoup
from selenium import webdriver
stock_file = r"C:\Users\Tut10\Desktop\PSTool-Python\Final\test.txt"
def Home_Depot_Shipping(url):
driver = webdriver.Chrome(r"C:\Users\Tut10\Desktop\PSTool-Python\chromedriver.exe")
driver.get(url)
# open a file to write to
file_to_write = open(stock_file, "a")
# send that file to me via email or text
free_delivery = driver.find_elements_by_xpath(r'//*[@id="buybelt"]/div[2]/div[2]/div/div[2]')
for x in free_delivery:
#This should print one of the following options
# Free Delivery, Standard Delivery, or Receive an email blah blah blah
try:
if "Free Delivery" in x.text:
driver.quit()
file_to_write.close()
return "\t\t[+] Free Delivery"
elif "Get it as soon as tomorrow" in x.text:
driver.quit()
#file_to_write.write("Cell: " + str(cell) + "[*] Express Delivery " + url + '\n')
file_to_write.write("[*] Express Delivery " + url + "\n")
file_to_write.close()
return "\t\t[*] Express Delivery"
elif "Receive an email" in x.text:
driver.quit()
file_to_write("[-] Out of Stock %s\n" % url)
file_to_write.close()
return "\t\t[-] Out of stock!"
elif "Standard Delivery" in x.text:
driver.quit()
file_to_write.close()
return "\t\t[+] Standard Delivery"
except Exception as e:
driver.quit()
print(e)
Home_Depot_Shipping(r"https://www.homedepot.com/p/WEN-8-in-5-Speed-Drill-Press-4208/204853910?keyword=wen+4208")
I expect the program to write to the file, if the delivery is express delivery (Get it as soon as tomorrow) or out of stock (Receive an email)
Just random, I know the code inside def Home_Depot_Shipping should be indented. It just wasn't copying right here. Ignore that please. It works fine for the Free delivery and standard. When it tries to write to the file though I keep getting this error
'_io.TextIOWrapper' object is not callable
Any suggestions or help would be greatly appreciated! Thanks
回答1:
perhaps you meant
file_to_write.write("[-] Out of Stock %s\n" % url)
instead of
file_to_write("[-] Out of Stock %s\n" % url)
as a general rule, error msgs have got good enough hints if you pay attention
来源:https://stackoverflow.com/questions/47625150/io-textiowrapper-object-is-not-callable