我想通过使用请求打开联机文件,然后将所有请求内容复制到本地文件,将文件内容从网页复制到本地文件。你知道吗
问题是,我在我的本地文件中得到的只是一个关于文件没有被UTF-8编码的抱怨。你知道吗
我需要先创建文件,然后再写入,所以到目前为止我尝试的是打开和关闭文件,以便写入文本并对其进行如下编码: f=打开(路径'w',编码'UTF-8') f、 关闭() 这不管用。你知道吗
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)
我希望该文件是一堆jibberish文件,就像其他以二进制打开的文件一样;但是,它只包含以下消息:
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
你必须在保存文件时这样做
相关问题 更多 >
编程相关推荐