Python:读取文件(来自外部服务器)

caw*_*caw 0 python file

你能告诉我如何编写从外部服务器读取文件的Python脚本吗?我寻找类似于PHP的file_get_contents()或file()函数的东西.

如果有人可以发布这样一个脚本的整个代码,那就太好了.

提前致谢!

Jar*_*die 12

整个脚本是:

import urllib
content = urllib.urlopen('http://www.google.com/').read()
Run Code Online (Sandbox Code Playgroud)

  • 您可能希望使用urllib2,它类似但提供了许多用于安全性和cookie处理的附加功能. (2认同)

Aar*_*ron 5

更好的是与Jarret的代码相同,但使用urllib2:

import urllib2
content = urllib2.urlopen('http://google.com').read()
Run Code Online (Sandbox Code Playgroud)

urllib2有点新,更现代.在你的情况下并不重要,但使用它是一个好习惯.