簡體   English   中英

修復將二進制數據寫入文件的問題:“文件未使用UTF-8編碼”

[英]Fix issue with writing binary data to file: 'file is not UTF-8 encoded'

我想通過使用請求打開在線文件,然后將我所有的請求內容都復制到本地文件中,從而將文件的內容從網頁復制到本地文件中。

問題是我在本地文件中得到的只是關於文件未采用UTF-8編碼的投訴。

我需要先創建文件,然后再寫入文件,因此到目前為止,我一直嘗試打開和關閉文件以寫入文本並進行如下編碼:f = open(path,'w',encoding ='UTF-8' )f.close()無效。

import requests
from bs4 import BeautifulSoup as soup
for i in range(3586, 9003, 95):
    print(i)
    #Get the link
    gal_links = requests.get(url + str(i))

    if not (gal_links.status_code == 200):
        print('DNE: ' + str(gal_links.status_code))

    else:
        scraper = soup(gal_links.text)
        href = scraper.find_all('a')
        #choose a random link starting after the 7th, and ending at nth - 2
        rand = randint(7, len(href)-2)
        star_link = href[rand]['href']
        file_url = url + str(i) + '/' + star_link
        print(file_url)

        #get the .fits file
        req = requests.get(file_url)


        path = '/Users/TheBestKid/Desktop/Hubble/fits/' + str(i) + '_' + str(rand) + '.fits'

        #write binary to local file
        #This is where I tried opening and closing the file for reading
        with open(path, 'wb+') as file:
            file.write(req.content)

我希望文件像一堆亂碼一樣,就像其他任何以二進制打開的文件一樣。 但是,它只包含以下消息:

Error! 'File_Name' is not UTF-8 encoded
Saving disabled
See console for more details

控制台輸出如下所示:

[W 01:10:00.905 NotebookApp] 
/Users/TheBestKid/Desktop/Hubble/fits/3586_539.fits is not UTF-8 
encoded
[W 01:10:00.905 NotebookApp] 400 GET 
/api/contents/fits/3586_539.fits? 
type=file&format=text&_=1562310600681 (::1) 1.45ms 
referer=http://localhost:8889/edit/fits/3586_539.fits

您必須在保存文件上執行此操作

file.write(req.content.encode("utf-8"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM