k_zeon Wrote:sorry , here is some code.
Code:
html = net.http_GET(url).content
match=re.compile('<li class="searchList"><a href="(.+?)">(.+?)</a> <span>(.+?)</span>').findall(html)
total = len(match)
for url,name,sYear in match:
if for example i only wanted the for to run 5 times only then exit for <<<<<<<
addon.add_directory({'mode' : 'GetMovieSource', 'url' : url , 'img' : '' }, CleanHTML(name) + ' ' + sYear, total_items=total)
addon.end_of_directory()
ok, cool, so match is a list of tuples and you only want to iterate through the first 5 tuples in the list. python has a cool feature called slices (
here is a really good clear explanation)
here is an example:
Code:
import re
html = '''<html>
<li class="searchList"><a href="http://url1">name1</a> <span>1901</span>
<li class="searchList"><a href="http://url2">name2</a> <span>1902</span>
<li class="searchList"><a href="http://url3">name3</a> <span>1903</span>
<li class="searchList"><a href="http://url4">name4</a> <span>1904</span>
<li class="searchList"><a href="http://url5">name5</a> <span>1905</span>
<li class="searchList"><a href="http://url6">name6</a> <span>1906</span>
<li class="searchList"><a href="http://url7">name7</a> <span>1907</span>
</html>
'''
matches = re.findall('<li class="searchList"><a href="(.+?)">(.+?)</a> <span>(.+?)</span>', html)
for match in matches[:5]:
print match # or do whatever else you want with it
if you run this you will see the following output:
Code:
('http://url1', 'name1', '1901')
('http://url2', 'name2', '1902')
('http://url3', 'name3', '1903')
('http://url4', 'name4', '1904')
('http://url5', 'name5', '1905')
the nice part is that even if nothing matches you will not get any errors (the code inside the for loop will just not be run at all)
also if less than five items are matched, the code in the for loop will only run on each match that does exist and not produce an error.
hope that helps!
t0mm0