简体   繁体   English

Python从URL中读取不含“ csv”后缀的csv文件

[英]Python Read csv file from URL without “csv” suffix

I'm trying to download a csv file from an url without the "csv" suffix. 我正在尝试从没有后缀“ csv”的URL下载一个csv文件。 The url is: https://www.ishares.com/de/professionelle-anleger/de/produkte/270048/ishares-msci-world-value-factor-ucits-etf/1478358465952.ajax?fileType=csv&fileName=IS3S_holdings&dataType=fund&asOfDate=20180731 网址为: https : //www.ishares.com/de/professionelle-anleger/de/produkte/270048/ishares-msci-world-value-factor-ucits-etf/1478358465952.ajax?fileType=csv&fileName=IS3S_holdings&dataType=基金&asOfDate = 20180731

Since there is no "csv" suffix I haven't found any solution for this problem. 由于没有“ csv”后缀,因此我没有找到解决此问题的方法。 My current code looks like this: 我当前的代码如下所示:

link = "https://www.ishares.com/de/professionelle-anleger/de/produkte/270048/ishares-msci-world-value-factor-ucits-etf/1478358465952.ajax?fileType=csv&fileName=IS3S_holdings&dataType=fund&asOfDate=20180731"
data = pd.read_csv(link)  

Any help is really appreciated. 任何帮助都非常感谢。 Thanks! 谢谢!

Try to look into the file. 尝试查看文件。 There's a header line before the data starts. 数据开始前有一个标题行。 Skiprows can help. Skiprows可以提供帮助。

data = pd.read_csv(link, skiprows=2) 

The code I tried : 我试过的代码:

import urllib2

link = "https://www.ishares.com/de/professionelle-anleger/de/produkte/270048/ishares-msci-world-value-factor-ucits-etf/1478358465952.ajax?fileType=csv&fileName=IS3S_holdings&dataType=fund&asOfDate=20180731"
local_file_name = 'test.csv'
u = urllib2.urlopen(link)
f = open(local_file_name, 'wb')
meta = u.info()

file_size_dl = 0
block_sz = 8192
while True:
    buffer = u.read(block_sz)
    if not buffer:
        break
    file_size_dl += len(buffer)
    f.write(buffer)

f.close()

Code executes an urlopen call to download the URL. 代码执行urlopen调用以下载URL。 Curiosly, the file it yields is an html: 有趣的是,它产生的文件是一个html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#" lang="de" xml:lang="de">
<head>
<title>iShares by BlackRock - Führender ETF Anbieter weltweit</title>
<link type="image/x-icon" href="//assets.blackrock.com/uk-retail-assets/ishares-

However, opening the same URL with a web browser allows to get the csv data... that csv, you can read it with pandas and 但是,使用网络浏览器打开相同的URL可以获取csv数据...该csv,您可以使用pandas和

 data = pd.read_csv(filename, skiprows=2, header=1)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM