How to get data from stdin using os.system ()

The only reliable method I've found to use a script to download text from wikipedia is cURL. So far, the only way to do this is to call os.system()

. Even though the output displays correctly in the python shell, I can't seem to get the function to return anything other than the exit code ( 0

). Alternatively someone can show you how to use it correctly urllib

.

0


source to share


3 answers


From Dive into Python:

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

      

This will print the source code for the Python Wikipedia article. I suggest you take a look at Dive in Python for more details.



Example using urllib2 from Python Library Reference:

import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)

      

Edit: Also you can have a look at wget.
Edit2: Added urllib2 example based on advice from S.Lott

+7


source


To answer the question, Python has a subprocess module that allows you to interact with spawned processes. http://docs.python.org/library/subprocess.html#subprocess.Popen

It allows you to read stdout for the called process and even send items to stdin.



however, as you said urllib is a much better option. if you search stackoverflow i am sure you will find at least 10 other related questions ...

+2


source


As an alternetive for urllib, you can use the libCurl Python bindings .

0


source







All Articles