_io.TextIOWrapper'对象不可调用

时间:2017-12-04 01:08:54

标签: python ioerror

我正在尝试打印以写入文件类型的运输和项目

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")

我希望程序写入文件,如果快递是快递(明天快递)或缺货(接收电子邮件)

随便,我知道def Home_Depot_Shipping里面的代码应该是缩进的。它只是不是在这里复制。请忽略。它适用于免费送货和标准。当它试图写入文件但我不断收到此错误

  

'_ io.TextIOWrapper'对象不可调用

任何建议或帮助将不胜感激! 感谢

1 个答案:

答案 0 :(得分:0)

也许你的意思是

file_to_write.write("[-] Out of Stock %s\n" % url)

而不是

file_to_write("[-] Out of Stock %s\n" % url)

作为一般规则,如果你注意,错误消息已经有足够的提示