将从网站抓取的文件上传到ftp服务器,pytjhon

问题描述 投票:0回答:1

我正在编写爬虫程序。我已经制作了搜寻器,可以从网页上搜寻新闻,它可以上传到我的本地计算机,但是我想直接上传到FTP服务器。

我尝试以多种方式编码。但我不能...

我的代码在下面python

for i in range(0,len(a),2):
    url = defaultInformation['gooktoHome'] + a[i].attrs['href']
    r = requests.get(
          url, allow_redirects=True)
    fileName = datetime.now().strftime('%Y%m%d%H%M%S')
    # print(r.text)
    if(a[i].attrs['href'][-3:] == 'pdf'):
        ftp.storbinary('STOR ' + '/uh/backup/' + fileName + '.pdf',open(r.content))
python web-crawler
1个回答
0
投票

我使用BytesIO解决了此问题我的代码在python下面]

for i in range(0,len(a),2):
    url = defaultInformation['gooktoHome'] + a[i].attrs['href']
    r = requests.get(
           url, allow_redirects=True)
    fileName = datetime.now().strftime('%Y%m%d%H%M%S')

    if(a[i].attrs['href'][-3:] == 'pdf'):
        tFile = BytesIO(r.content)
        ftp.storbinary('STOR ' + '/uh/backup/' + fileName + '.pdf',tFile)
    
© www.soinside.com 2019 - 2024. All rights reserved.