从网上下载excel文件? [英] Download excel file from web?

查看:94
本文介绍了从网上下载excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 - 有经验的程序员,但这是我的第一个Python程序。


此URL将检索包含(当天)的excel电子表格

msci股票指数回归。

http://www.mscibarra.com/webapp/inde...EIPerfRegional


想编写python来下载并保存文件。


到目前为止,我已经达到了这个目的:


#import pdb

import urllib2

来自win32com.client import Dispatch


xlApp = Dispatch(" Excel.Application")

#test 1

#xlApp.Workbooks.Add()

#xlApp.ActiveSheet.Cells(1,1).Value =''A''

#xlApp.ActiveWorkbook.ActiveSheet.Cel ls(2,1).Value =''B''

#xlBook = xlApp.ActiveWorkbook

#xlBook.SaveAs(Filename =''C:\\\ \\ test.xls'')

#pdb.set_trace()

response = urllib2.urlopen('''http://www.mscibarra.com/webapp/indexperf /

excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')

#test 2 - 返回check = False

check_for_data = urllib2.Request('' http://www.mscibarra.com/webapp/

indexperf / excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')。has_dat a()


xlApp = response.fp

print(response.fp.name)

print(xlApp.name)

xlApp.write

xlApp.Close

解决方案

7月28日,3:00 * pm,p ... @ well.com < p ... @ well.comwrote:


嗨 - 有经验的程序员,但这是我的第一个Python程序。


此URL将检索包含(当天)的excel电子表格

msci股票指数返回。

http://www.mscibarra.com/webapp/inde...vel=0&scope=0& ....


想编写python来下载并保存文件。


到目前为止我已经达到了这个目的:


#import pdb

import urllib2

来自win32com.client import Dispatch


xlApp = Dispatch(" Excel。应用程序)


#test 1

#xlApp.Workbooks.Add()

#xlApp.ActiveSheet.Cells(1 ,1).Value =''A''

#xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value =''B''

#xlBook = xlApp.ActiveWorkbook

#xlBook.SaveAs(Filename =''C:\\test.xls'')


#pdb.set_trace( )

response = urllib2.urlopen('''http://www.mscibarra.com/webapp/indexperf/

excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 200 8& export = Excel_IEIPerfRegional'')

#test 2 - 返回check = False

check_for_data = urllib2.Request('''http://www.mscibarra.com/ webapp /

indexperf / excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')。has_dat a()


xlApp = response.fp

print(response.fp.name)

print(xlApp.name)

xlApp.write

xlApp.Close



Woops在我想要预览时点击发送。看起来像html [quote]标签

无法使用groups.google.com(不错)。


Anway,在上面的测试1中,我决定如何实例化一个excel

对象;把一些东西放进去;然后保存到磁盘。


所以,从理论上讲,我正在检索我的Excel电子表格


response = urllib2.urlopen()


除了我对此做了什么?


好​​好阅读一些urllib2文档并找到

请求类使用方法has_data()。它返回False。

嗯,这不令人鼓舞。


我想要了解urllib2.urlopen返回的技巧

给我;在那里翻找;并希望找到我的excel文件。


我使用pdb进行调试。这很有意思:


(Pdb)目录(回复)

[''__doc__'',''_ _ init _''''''__ item__'', ''__module__'',''_ _ jpr'''''''关闭'',

''代码'',''

fileno'',''fp' ',''geturl'',''header'',''info'',''msg'',''next'',''read'',

''readline' ',''

readlines'',''url'']

(Pdb)


我想成员们__ * _是方法;没有

下划线的名字是属性(变量)(?)。


或者这可能不是正确的方向(也许那里

是做这个东西的更好的模块)。很高兴知道如果是这样的话(如果能为我完成工作的话)(

)。


pat


pa**@well.com schrieb:[quote]

7月28日下午3点,p ... @ well.com < p ... @ well.comwrote:


>嗨 - 有经验的程序员,但这是我的第一个Python程序。

这个URL将检索一个excel电子表格,其中包含(当天)的msci股票指数返回。

http://www.mscibarra.com/webapp/inde...vel=0&scope=0& ...

想要编写python到下载并保存文件。

到目前为止,我已经达到了这个目的:


> #import pdb <从win32com.client导入发送urllib2


xlApp = Dispatch(" Excel.Application")

#test 1
# xlApp.Workbooks.Add()
#xlApp.ActiveSheet.Cells(1,1).Value =''A''
#xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = ''B''
#xlBook = xlApp.ActiveWorkbook
#xlBook.SaveAs(Filename =''C:\\test.xls'')

#pdb .set_trace()
response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& market = 1897& asOf = Jul
+ 25%2C + 2008& export = Excel_IEIPerfRegional'')
#test 2 - return check = False
check_fo r_data = urllib2.Request(''http://www.mscibarra.com/webapp/
indexperf / excel?
priceLevel = 0& scope = 0& currency = 15& style = C& size = 36安培; market = 1897& asOf = Jul
+ 25%2C + 2008& export = Excel_IEIPerfRegional'')。has_da ta()

xlApp = response.fp
print(响应。 fp.name)
print(xlApp.name)
xlApp.write
xlApp.Close



Woops hit我想要预览时发送。看起来像html


标签

无法使用groups.google.com(不错)。


Anway,在上面的测试1中,我确定了如何实例化excel

对象;把一些东西放进去;然后保存到磁盘。


所以,从理论上讲,我正在检索我的Excel电子表格


response = urllib2.urlopen()


除了我对此做了什么?


好​​好阅读一些urllib2文档并找到

请求类使用方法has_data()。它返回False。

嗯,这不令人鼓舞。


我想要了解urllib2.urlopen返回的技巧

给我;在那里翻找;并希望找到我的excel文件。


我使用pdb进行调试。这很有意思:


(Pdb)目录(回复)

[''__doc__'',''_ _ init _''''''__ item__'', ''__module__'',''_ _ jpr'''''''关闭'',

''代码'',''

fileno'',''fp' ',''geturl'',''header'',''info'',''msg'',''next'',''read'',

''readline' ',''

readlines'',''url'']

(Pdb)


我想成员们__ * _是方法;没有

下划线的名称是属性(变量)(?)。



不,这些是所有属性和方法的名称。 read是一种方法,例如


或者这可能不是正确的方向(也许在那里

是做这个东西的更好的模块)。很高兴得知

就是这种情况(如果能让我完成工作)。



文档( http://docs.python.org/lib/module-urllib2.html 非常好

清楚:


"""

此函数返回一个类似文件的对象,有两种方法:

"""

然后是类似文件的对象:

http://docs.python.org/lib/bltin-file-objects.html

"""

read([ size])

从文件中读取最多大小字节(如果读取在获取大小字节之前达到EOF

,则更少)。如果size参数为负数或

省略,则读取所有数据,直到达到EOF。字节作为

字符串对象返回。遇到EOF时会返回一个空字符串

。 (对于某些文件,比如ttys,在EOF被击中后继续

读取是有意义的。)注意这个方法可能会调用

底层C函数fread()不止一次努力获得尽可能接近大小字节的
。另请注意,当处于非阻塞状态时,即使没有给出大小

参数,也可以返回比请求数据少的数据。

""


Diez


7月28日,3:29 * pm,Diez B .Roggisch < de ... @nospam.web.dewrote:[quote]

p ... @ well.com schrieb:


7月28日下午3点,p ... @ well.com < p ... @ well.comwrote:


嗨 - 有经验的程序员,但这是我的第一个Python程序。


此URL将检索包含(当天)的excel电子表格

msci股指回报。


> http://www.mscibarra.com/webapp/inde...vel=0&scope=0& ...

< blockquote class =post_quotes>


想编写python来下载并保存文件。


到目前为止,我已经到达:


#import pdb

import urllib2

来自win32com.client import Dispatch


xlApp = Dispatch(" Excel.Application" ;)


#test 1

#xlApp.Workbooks。添加()

#xlApp.ActiveSheet.Cells(1,1).Value =''A''

#xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1 ).Value ='''B''

#xlBook = xlApp.ActiveWorkbook

#xlBook.SaveAs(Filename =''C:\\ test.xls' ')


#pdb.set_trace()

response = urllib2.urlopen('''http://www.mscibarra.com/webapp/indexperf /

excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')

#test 2 - 返回check = False

check_for_data = urllib2.Request('' http://www.mscibarra.com/webapp/

indexperf / excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')。has_dat a()


xlApp = response.fp

print(response.fp.name)

print(xlApp .name)

xlApp.write

xlApp.Close


Woops在我想要预览时点击发送。 *看起来像html


标签

无法使用groups.google.com(不错)。


Anway,在上面的测试1中,我确定了如何实例化excel

对象;把一些东西放进去;然后保存到磁盘。


因此,理论上,我正在使用

检索我的Excel电子表格
response = urllib2.urlopen()


除了我对此做什么之外?


好​​好阅读一些urllib2文档并找到

Request类,其上有方法has_data() 。 *它返回False。

嗯,这并不令人鼓舞。


我想知道urllib2.urlopen回归给我的诀窍

给我;在那里翻找;并希望找到我的excel文件。


我使用pdb进行调试。 *这很有趣:


(Pdb)dir(响应)

[''__doc__'',''__ init__ '',''__ item__'',''_ _ _ _ _ _ _ _''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' b fileno'',''fp'',''geturl'',''header'',''info'',''msg'',''next'',''read'',

''readline'',''

readlines'',''url'']

(Pdb)


我认为__ * _的成员是方法;没有

下划线的名称是属性(变量)(?)。



不,这些是所有属性和方法的名称。 read是一种方法,例如



对 - 我倒退了。


>


或许这可能没有正确的方向(也许那里

是更好的模块来做这些事情) 。 *很高兴能够了解是否确实如此(如果能够为我完成工作)。



文档( http://docs.python.org/lib/module-urllib2.html 非常好

清楚:


"""

此函数返回一个类似文件的对象,其中包含两种方法:

"""


然后是类似文件的对象:

http://docs.python.org/lib/bltin-file-objects.html


读取(* [size])

* * *从文件中读取大多数大小字节(如果读取到达EOF

,则在获取大小字节之前减去)。如果size参数为负数或

省略,则读取所有数据,直到达到EOF。字节作为

字符串对象返回。遇到EOF时会返回一个空字符串

。 (对于某些文件,比如ttys,在EOF被击中后继续

读取是有意义的。)注意这个方法可能会调用

底层C函数fread()不止一次努力获得尽可能接近大小字节的
。另请注意,当处于非阻塞状态时,即使没有给出大小

参数,也可以返回比请求数据少的数据。

"""


Diez



偶然发现.read:


response = urllib2.urlopen('''http://www.mscibarra.com/webapp/indexperf/

excel?

priceLevel = 0& scope = 0& currency = 15& style = C& size = 36& m arket = 1897& asOf = Jul

+ 25%2C + 2008& export = Excel_IEIPerfRegional'')。阅读


现在问题是:该怎么办?我会查看您指向的

文档。


thanx - pat


Hi - experienced programmer but this is my first Python program.

This URL will retrieve an excel spreadsheet containing (that day''s)
msci stock index returns.

http://www.mscibarra.com/webapp/inde...EIPerfRegional

Want to write python to download and save the file.

So far I''ve arrived at this:

# import pdb
import urllib2
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")

# test 1
# xlApp.Workbooks.Add()
# xlApp.ActiveSheet.Cells(1,1).Value = ''A''
# xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = ''B''
# xlBook = xlApp.ActiveWorkbook
# xlBook.SaveAs(Filename=''C:\\test.xls'')
# pdb.set_trace()
response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'')
# test 2 - returns check = False
check_for_data = urllib2.Request(''http://www.mscibarra.com/webapp/
indexperf/excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'').has_dat a()

xlApp = response.fp
print(response.fp.name)
print(xlApp.name)
xlApp.write
xlApp.Close

解决方案

On Jul 28, 3:00*pm, "p...@well.com" <p...@well.comwrote:

Hi - experienced programmer but this is my first Python program.

This URL will retrieve an excel spreadsheet containing (that day''s)
msci stock index returns.

http://www.mscibarra.com/webapp/inde...vel=0&scope=0&....

Want to write python to download and save the file.

So far I''ve arrived at this:

# import pdb
import urllib2
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")

# test 1
# xlApp.Workbooks.Add()
# xlApp.ActiveSheet.Cells(1,1).Value = ''A''
# xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = ''B''
# xlBook = xlApp.ActiveWorkbook
# xlBook.SaveAs(Filename=''C:\\test.xls'')

# pdb.set_trace()
response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'')
# test 2 - returns check = False
check_for_data = urllib2.Request(''http://www.mscibarra.com/webapp/
indexperf/excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'').has_dat a()

xlApp = response.fp
print(response.fp.name)
print(xlApp.name)
xlApp.write
xlApp.Close

Woops hit Send when I wanted Preview. Looks like the html [quote] tag
doesn''t work from groups.google.com (nice).

Anway, in test 1 above, I determined how to instantiate an excel
object; put some stuff in it; then save to disk.

So, in theory, I''m retrieving my excel spreadsheet with

response = urllib2.urlopen()

Except what then do I do with this?

Well for one read some of the urllib2 documentation and found the
Request class with the method has_data() on it. It returns False.
Hmm that''s not encouraging.

I supposed the trick to understand what urllib2.urlopen is returning
to me; rummage around in there; and hopefully find my excel file.

I use pdb to debug. This is interesting:

(Pdb) dir(response)
[''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''__repr__'', ''close'',
''code'', ''
fileno'', ''fp'', ''geturl'', ''headers'', ''info'', ''msg'', ''next'', ''read'',
''readline'', ''
readlines'', ''url'']
(Pdb)

I suppose the members with __*_ are methods; and the names without the
underbars are attributes (variables) (?).

Or maybe this isn''t at all the right direction to take (maybe there
are much better modules to do this stuff). Would be happy to learn if
that''s the case (and if that gets the job done for me).

pat


pa**@well.com schrieb:[quote]
On Jul 28, 3:00 pm, "p...@well.com" <p...@well.comwrote:

>Hi - experienced programmer but this is my first Python program.

This URL will retrieve an excel spreadsheet containing (that day''s)
msci stock index returns.

http://www.mscibarra.com/webapp/inde...vel=0&scope=0&...

Want to write python to download and save the file.

So far I''ve arrived at this:

># import pdb
import urllib2
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")

# test 1
# xlApp.Workbooks.Add()
# xlApp.ActiveSheet.Cells(1,1).Value = ''A''
# xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = ''B''
# xlBook = xlApp.ActiveWorkbook
# xlBook.SaveAs(Filename=''C:\\test.xls'')

# pdb.set_trace()
response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel=0&scope=0&currency=15&style=C&size=36& market=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'')
# test 2 - returns check = False
check_for_data = urllib2.Request(''http://www.mscibarra.com/webapp/
indexperf/excel?
priceLevel=0&scope=0&currency=15&style=C&size=36& market=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'').has_da ta()

xlApp = response.fp
print(response.fp.name)
print(xlApp.name)
xlApp.write
xlApp.Close


Woops hit Send when I wanted Preview. Looks like the html

tag
doesn''t work from groups.google.com (nice).

Anway, in test 1 above, I determined how to instantiate an excel
object; put some stuff in it; then save to disk.

So, in theory, I''m retrieving my excel spreadsheet with

response = urllib2.urlopen()

Except what then do I do with this?

Well for one read some of the urllib2 documentation and found the
Request class with the method has_data() on it. It returns False.
Hmm that''s not encouraging.

I supposed the trick to understand what urllib2.urlopen is returning
to me; rummage around in there; and hopefully find my excel file.

I use pdb to debug. This is interesting:

(Pdb) dir(response)
[''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''__repr__'', ''close'',
''code'', ''
fileno'', ''fp'', ''geturl'', ''headers'', ''info'', ''msg'', ''next'', ''read'',
''readline'', ''
readlines'', ''url'']
(Pdb)

I suppose the members with __*_ are methods; and the names without the
underbars are attributes (variables) (?).

No, these are the names of all attributes and methods. read is a method,
for example.

Or maybe this isn''t at all the right direction to take (maybe there
are much better modules to do this stuff). Would be happy to learn if
that''s the case (and if that gets the job done for me).


The docs (http://docs.python.org/lib/module-urllib2.html) are pretty
clear on this:

"""
This function returns a file-like object with two additional methods:
"""
And then for file-like objects:

http://docs.python.org/lib/bltin-file-objects.html
"""
read( [size])
Read at most size bytes from the file (less if the read hits EOF
before obtaining size bytes). If the size argument is negative or
omitted, read all data until EOF is reached. The bytes are returned as a
string object. An empty string is returned when EOF is encountered
immediately. (For certain files, like ttys, it makes sense to continue
reading after an EOF is hit.) Note that this method may call the
underlying C function fread() more than once in an effort to acquire as
close to size bytes as possible. Also note that when in non-blocking
mode, less data than what was requested may be returned, even if no size
parameter was given.
"""

Diez


On Jul 28, 3:29*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:[quote]
p...@well.com schrieb:

On Jul 28, 3:00 pm, "p...@well.com" <p...@well.comwrote:

Hi - experienced programmer but this is my first Python program.

This URL will retrieve an excel spreadsheet containing (that day''s)
msci stock index returns.

>http://www.mscibarra.com/webapp/inde...vel=0&scope=0&...

Want to write python to download and save the file.

So far I''ve arrived at this:

# import pdb
import urllib2
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")

# test 1
# xlApp.Workbooks.Add()
# xlApp.ActiveSheet.Cells(1,1).Value = ''A''
# xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = ''B''
# xlBook = xlApp.ActiveWorkbook
# xlBook.SaveAs(Filename=''C:\\test.xls'')

# pdb.set_trace()
response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'')
# test 2 - returns check = False
check_for_data = urllib2.Request(''http://www.mscibarra.com/webapp/
indexperf/excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'').has_dat a()

xlApp = response.fp
print(response.fp.name)
print(xlApp.name)
xlApp.write
xlApp.Close

Woops hit Send when I wanted Preview. *Looks like the html

tag
doesn''t work from groups.google.com (nice).

Anway, in test 1 above, I determined how to instantiate an excel
object; put some stuff in it; then save to disk.

So, in theory, I''m retrieving my excel spreadsheet with

response = urllib2.urlopen()

Except what then do I do with this?

Well for one read some of the urllib2 documentation and found the
Request class with the method has_data() on it. *It returns False.
Hmm that''s not encouraging.

I supposed the trick to understand what urllib2.urlopen is returning
to me; rummage around in there; and hopefully find my excel file.

I use pdb to debug. *This is interesting:

(Pdb) dir(response)
[''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''__repr__'', ''close'',
''code'', ''
fileno'', ''fp'', ''geturl'', ''headers'', ''info'', ''msg'', ''next'', ''read'',
''readline'', ''
readlines'', ''url'']
(Pdb)

I suppose the members with __*_ are methods; and the names without the
underbars are attributes (variables) (?).


No, these are the names of all attributes and methods. read is a method,
for example.

right - I got it backwards.

>

Or maybe this isn''t at all the right direction to take (maybe there
are much better modules to do this stuff). *Would be happy to learn if
that''s the case (and if that gets the job done for me).


The docs (http://docs.python.org/lib/module-urllib2.html) are pretty
clear on this:

"""
This function returns a file-like object with two additional methods:
"""

And then for file-like objects:

http://docs.python.org/lib/bltin-file-objects.html

"""
read( * [size])
* * *Read at most size bytes from the file (less if the read hits EOF
before obtaining size bytes). If the size argument is negative or
omitted, read all data until EOF is reached. The bytes are returned as a
string object. An empty string is returned when EOF is encountered
immediately. (For certain files, like ttys, it makes sense to continue
reading after an EOF is hit.) Note that this method may call the
underlying C function fread() more than once in an effort to acquire as
close to size bytes as possible. Also note that when in non-blocking
mode, less data than what was requested may be returned, even if no size
parameter was given.
"""

Diez

Just stumbled upon .read:

response = urllib2.urlopen(''http://www.mscibarra.com/webapp/indexperf/
excel?
priceLevel=0&scope=0&currency=15&style=C&size=36&m arket=1897&asOf=Jul
+25%2C+2008&export=Excel_IEIPerfRegional'').read

Now the question is: what to do with this? I''ll look at the
documentation that you point to.

thanx - pat


这篇关于从网上下载excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆