I have a script, that puts a number (in this case "9") into a var and saves it as a .txt file. Now i wanted to store that number automatically in a .txt file on my FTP server.
connecting to FTP works and i managed to create the "lifetime_wins.txt" file. But it has nothing in it.
I want to store the data of the "headers" variable in the txt. can anyone help here? I am not that good at python :x
Code:
import bs4
import time
def main():
#----INPUT BASIC----
#import bs4 and url-grabbing-module
from urllib.request import urlopen as uReq
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup
#setting var "url" to the page
url = 'https://fortnitetracker.com/profile/psn/Rehgum'
#var req to uReq and setting Mozilla / decoding for https
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
#decoding
web_byte = urlopen(req).read()
webpage = web_byte.decode('utf-8')
urlopen(req).close()
#html parsing
page_soup = soup(webpage, "html.parser")
#----SORTING----
#selecting <div> lifetime
lifetime = page_soup.findAll("div", {"class": "top-stats"})
#headers and file creation
filename = "lifetime_wins.txt"
f = open(filename, "w")
#setting lifetime into another var (0)
stats = lifetime[0]
#selecting container with the right information
stats.div.div
#setting variable to put in the value
value = stats.div.div
#----OUTPUT----
output = value.text
#----PRINTING OUTPUT----
print("Lifetime Siege: " + output)
headers = ("#" + output + " ")
f.write(headers)
f.close()
#---FTP CONNECTION---
from ftplib import FTP
ftp = FTP('server.1and1-data.host')
ftp.login('user','pw')
ftp.cwd('/')
#--WRITING HEADERS TO TXT ON FTP--
def placeFile():
filename = 'lifetime_wins.txt'
ftp.storbinary('STOR '+filename, f.write(headers), open(filename, 'rb'))
f.close()
ftp.quit()
#---------------------------------
#loop
time.sleep(30)
main()
for i in range(1, 2):
main()
while i < 1:
print("Ende")
Edit:
Its working after changing the method from storing a var into the text to storing the file first and then upload it. Post was not a duplicate, as i don't wanted to upload a file but changing some content of a file...
Working Code:
import bs4
import time
import ftplib
def main():
#----INPUT BASIC----
#import bs4 and url-grabbing-module
from urllib.request import urlopen as uReq
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup
#setting var "url" to the page
url = 'https://fortnitetracker.com/profile/psn/Rehgum'
#var req to uReq and setting Mozilla / decoding for https
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
#decoding
web_byte = urlopen(req).read()
webpage = web_byte.decode('utf-8')
urlopen(req).close()
#html parsing
page_soup = soup(webpage, "html.parser")
#----SORTING----
#selecting <div> lifetime
lifetime = page_soup.findAll("div", {"class": "top-stats"})
#headers and file creation
filename = "lifetime_wins.txt"
f = open(filename, "w")
#setting lifetime into another var (0)
stats = lifetime[0]
#selecting container with the right information
stats.div.div
#setting variable to put in the value
value = stats.div.div
#----OUTPUT----
output = value.text
#----PRINTING OUTPUT----
print("Lifetime Siege: " + output)
headers = ("#" + output + " ")
f.write(headers)
f.close()
#---FTP CONNECTION---
from ftplib import FTP
ftp = FTP('SERVER.1and1-data.host')
ftp.login('USER','PW')
ftp.cwd('/')
#--UPLOADING TXT ON FTP--
file = open('lifetime_wins.txt','rb')
ftp.storbinary('STOR ' +"lifetime_wins.txt", file)
file.close()
ftp.quit()
#loop
time.sleep(30)
main()
for i in range(1, 2):
main()
while i < 1:
print("Ende")