我正在尝试打印将什么类型的运输和物品写入文件
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'对象不可调用
任何建议或帮助将不胜感激!
谢谢
最佳答案
也许你是说
file_to_write.write("[-] Out of Stock %s\n" % url)
代替
file_to_write("[-] Out of Stock %s\n" % url)
作为一般规则,如果您注意错误消息,则会得到足够好的提示
关于python - _io.TextIOWrapper'对象不可调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47625150/