使用Python的ftplib将编号(var)写入FTP服务器上的.txt文件

2024-04-26 09:54:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它将一个数字(在本例中为“9”)放入一个var中,并将其保存为一个.txt文件。现在我想把这个数字自动存储在FTP服务器上的一个.txt文件中。你知道吗

连接到FTP works,我成功地创建了“生命周期”_wins.txt文件“文件。 但里面什么都没有。你知道吗

我想将“headers”变量的数据存储在txt中。 有人能帮忙吗?我不太擅长python:x

代码:

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

编辑:

它的工作方式是将var存储到文本中的方法更改为先存储文件,然后再上传。 帖子不是重复的,因为我不想上传一个文件,但改变了一个文件的一些内容。。。你知道吗

工作代码:

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

Tags: fromimportdivtxturlvarstatsftp