Ich versuche, eine '.xlsx' Datei von der Tax Foundation Website zu kratzen. Leider bekomme ich weiterhin eine Fehlermeldung, die lautet: Excel cannot open the file '2017-FF-For-Website-7-10-2017.xlsx because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extension matches the format of the file
. Ich habe etwas recherchiert und es sagt, der Weg, dies zu beheben, besteht darin, die Dateierweiterung in '.xls' anstatt '.xlsx' zu ändern. Kann jemand helfen?Wie man die Dateiendung ändert?
from bs4 import BeautifulSoup
import urllib.request
import os
url = urllib.request.urlopen("https://taxfoundation.org/facts-figures-2017/")
soup = BeautifulSoup(url, from_encoding=url.info().get_param('charset'))
FHFA = os.chdir('C:/US_Census/Directory')
seen = set()
for link in soup.find_all('a', href=True):
href = link.get('href')
if not any(href.endswith(x) for x in ['.xlsx']):
continue
file = href.split('/')[-1]
filename = file.rsplit('.', 1)[0]
if filename not in seen: # only retrieve file if it has not been seen before
seen.add(filename) # add the file to the set
url = urllib.request.urlretrieve('https://taxfoundation.org/' + href, file)
print(filename)
print(' ')
print("All files successfully downloaded.")
P.S. Ich weiß, dass Sie die Datei herunterladen können, aber ich bin Web-Scraping, um einen bestimmten Prozess zu automatisieren.
Welche Version von Python verwenden Sie? – TheDetective
Diese Anweisung für die Schleife 'falls nicht vorhanden (href.endswith (x) für x in ['.xlsx'])' läuft einmal 'für x in [' .xlsx '] 'und prüft dann, ob' href.endswith ('xlsx') '. Sie können das im Prinzip mit 'falls nicht href.endswith ('. Xlsx') verkürzen, was einfacher ist. – Vinny
Ich benutze Python 3.6 @TheDetective – bhammer