0

import re, urllib

def get_files(page):
    a = urllib.urlopen(page)
    b = a.read()
    c = re.findall("([a-zA-Z0-9]+\.{1}(jpg|bmp|docx|gif))",b)
    return c 
def main():
    print get_files("http://www.soc.napier.ac.uk/~40001507/CSN08115/cw_webpage/index.html")

if __name__ == "__main__":
    main()

After I ran this code, I had an issue with its regex, hence the answer will be like this:

[('clown.gif', 'gif'), ('sleeper.jpg', 'jpg'), ('StarWarsReview.docx', 'docx'), ('wargames.jpg', 'jpg'), ('nothingtoseehere.docx', 'docx'), ('starwars.jpg', 'jpg'), ('logo.jpg', 'jpg'), ('certified.jpg', 'jpg'), ('clown.gif', 'gif'), ('essays.gif', 'gif'), ('big.jpg', 'jpg'), ('Doc100.docx', 'docx'), ('FavRomComs.docx', 'docx'), ('python.bmp', 'bmp'), ('dingbat.jpg', 'jpg')]

I don't want the result to be like this ('clown.gif', 'gif') all I want it to be like is ['clown.gif','sleeper.jpg'] and so on

Is there anyway to do it?? and get red of the tuple??

ibr2
  • 51
  • 9

2 Answers2

1

You just need to turn your group into a non-capturing group.

def get_files(page):
    a = urllib.urlopen(page)
    b = a.read()
    c = re.findall("([a-zA-Z0-9]+\.{1}(?:jpg|bmp|docx|gif))", b)
Community
  • 1
  • 1
metatoaster
  • 17,419
  • 5
  • 55
  • 66
0

you are doing double capturing of the extension, try with regex below, ?: means non-capturing group

re.findall("([a-zA-Z0-9]+\.{1}(?:jpg|bmp|docx|gif))", b)

i simplify your regex to below, the {1} seems redundant, and using \w and \d for word and number group

re.findall("([\w\d]+\.(?:jpg|bmp|docx|gif))", b)
Skycc
  • 3,496
  • 1
  • 12
  • 18