-3

I am currently learning Python, and could really use help from experienced coders with help to getting started on solving this assignment:

  1. Using os.walk, write a script that will print the filenames of zero length files. It should also print the count of zero length files.

  2. Write a script that will list and count all of the images in a given HTML web page/file. You can assume that:

       Each image file is enclosed with the tag <img and ends with >
       The HTML page/file is syntactically correct
    

Any input is much appreciated!

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • 1
    You should at least show your attempts at solving these. – DeepSpace Aug 04 '16 at 10:32
  • This is really two distinct questions/requests for code. I bet there are existing questions on walking files and parsing html. e.g. http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python/3207973#3207973 You could then read the manual for properties of the file – doctorlove Aug 04 '16 at 10:34

1 Answers1

0

You can use BeautifulSoup to easily count the number of images on the page. All you would need to do is scrape all of the tags and get the length of that scrape.

import urllib
from bs4 import BeautifulSoup

url = 'whatever the website is'
r = urllib.urlopen(url).read()
soup = BeautifulSoup(r, 'html.parser')

num_images = len(soup.find_all('img'))
print num_images

This code hasn't been compiled. I don't think it's entirely accurate, but it should give you more than enough of an idea on how to do it.

Better yet would be to take a look at this SO post, specifically the answer that i've linked, which has an implementation using regex: https://stackoverflow.com/a/17395503/6464893

Community
  • 1
  • 1
Harrison
  • 5,095
  • 7
  • 40
  • 60