我正在制作一個爬蟲程序。我制作了從網頁抓取新聞的爬蟲,它可以上傳到我的本地計算機,但我想直接上傳到 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))
1 回答

largeQ
TA貢獻2039條經驗 獲得超8個贊
我使用 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)
添加回答
舉報
0/150
提交
取消