Sunday, 11 January 2015

Reading from URL in Python

This code snippet gives a demo of

  • creating and importing module
  • raw string to ignore escape characters in a string e.g. file location
  • reading bytes from a URL
  • parsing bytes and converting them into string words

Code

UrlHandler.py
__author__ = 'akhtar'

from urllib.request import urlopen

def rawStringExample():
    path = r"C:\Program Files\Microsoft Office\Office14\1033"
    print(path)

def readingURL(url='http://techmightsolutions.blogspot.com/'):
    story_words =[]
    with urlopen(url) as story:
        for line in story:
            line_word = line.decode('utf-8').split()
            for word in line_word:
                story_words.append(word)
    return story_words

def getWordCount(url='http://techmightsolutions.blogspot.com/'):
    return len(str(readingURL(url)))

__init__.py
__author__ = 'akhtar'

from com.techmight.UrlHandler import readingURL, getWordCount


def main():
    print(readingURL())
    print(getWordCount("http://timesofindia.indiatimes.com"))


if __name__ == '__main__':
    main()

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?