python - Read text data from a website -


my program recursively processes string reverse it. have pull data directly website instead of text file does, can't pull data website.

import urllib.request  def reverse(alist):     #print(alist)     if alist == []:         return []     else:         return reverse(alist[1:]) + [alist[0]]  def main():     #file1 = urllib.request.urlopen('http://devel.cs.stolaf.edu/parallel/data/cathat.txt').read()     file1 = open('cat.txt','r')     line in file1:         stulist = line.split()         x = reverse(stulist)         print(' '.join(x))     file1.close()  main() 

the commented-out lines show have tried.

you can use url file:

import urllib ... f = urllib.urlopen(url) line in f:     ... f.close() 

what did call read on opened url. read content file1 variable , file1 became string.

for python 3:

import urllib.request ... f = urllib.request.urlopen(url) line in f:     ... f.close() 

also need convert each line correct encoding. if encoding utf-8 can following:

for line in f:     line = line.decode("utf-8") 

Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -