_io.TextIOWrapper' object is not callable

牧云@^-^@ 提交于 2021-01-28 10:07:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!