Tuesday, 15 May 2012

python - pull specific iteration output from for loop -


I am writing a function that snatch posts from the website www.meh.ro. I want to draw a random post from this random page, but the way I created it, it runs all the posts again on the HTML with the loop, and I need to return output from a single post. I am searching for a simple solution and breaking my head, but I have writers' blocks, I think. I was hoping someone has a great idea that I am missing.

My code: urlib import urlopen from random import from the random number # urlib import urlretrieve to bs4 import beautiful soup hit = false while kicking == incorrect: link = ' Http://www.meh.ro/page/ '+ str (RANDIN (1, 1000)) print link, \ n --- \ n \ n' try: source = urlopen (link) .read () soup = Soup. Beautiful soup (source) for tag in 'rounded' (source): Try: if tag ['class'] [1] == 'post': # print tag. Prettify ('utf-8') , '\ N \ n' title = tag. H2.a.string imageURL = tag.pa ['href'] sourceURL = tag.div.a ['href']. Partition ('#') [0] Print Title print imageURL print sourceURL Print '\ n' hit = exception Except exception, e: if type (e)! = 'Exceptions.IndexError' or 'exceptions.KeyError': print 'try2:', type (e), '\ n', except e exception, e: print 'try1:', type (e), '\ N ', e

Based on an idea I considered doing this, I had chosen a specific entry to determine the possibility of using elsewhere in my code, the list In order to add n times, I (9) for ('' imgur ()) == [] im.exe] Returns (10)] Reverse Reward (L [RANDIN (0, LEN (L) -1) ]) Return P> doing this work Singing, but I could be a better solution'm asking close-around because I'm sure someone else experience than me.

To take a post at random, you should still have a loop between them and give them a The list must be collected in:

  Random post for import = [] tags in Soup.find_all ('div', class_ = 'post'): title = tag.h2.a .string imageURL = tag.pa ['href'] sourceURL = tag.div.a ['href']. The post ('#', 1) [0] post .append (title, imageURL, sourceURL)) title, imageURL, sourceURL = random.choice (posts)   

Use random.choice () to collect a random entry in the list of Collects (Title, Image URL, Source URL).

No comments:

Post a Comment