如何使用os.system()从stdin获取数据

Gam*_*eak 0 python shell curl os.system urllib

我使用脚本从维基百科下载文本的唯一可靠方法是使用cURL.到目前为止,我这样做的唯一方法是打电话os.system().即使输出在python shell中正确显示,我似乎无法返回除退出代码(0)以外的任何内容.或者有人可以证明如何正确使用urllib.

Sea*_*ean 7

Dive到Python:

import urllib
sock = urllib.urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)")
htmlsource = sock.read()
sock.close()
print htmlsource
Run Code Online (Sandbox Code Playgroud)

这将打印出Python Wikipedia文章的源代码.我建议你看看Dive into Python了解更多细节.

使用Python Library Reference中的 urllib2的示例:

import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)
Run Code Online (Sandbox Code Playgroud)

编辑:你也可以看看wget.
Edit2:根据S.Lott的建议添加了urllib2示例