Advertisement
naimul64

Quora_Beautiful_Soup_famous_ans

Jul 13th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import urllib
  3. r = urllib.urlopen('file:///home/insan/Documents/zz_GURBAGE/bs.html').read()
  4. soup = BeautifulSoup(r,"lxml")
  5. mydivs = soup.findAll("div", { "class" : "Answer AnswerBase" })
  6. print len(mydivs)
  7. howAmI = []
  8. for div in mydivs:
  9.     user = div.findAll("a", { "class" : "user" })[0].string
  10.     print user.strip
  11.  
  12.  
  13.  
  14.    
  15.     try:
  16.         view = div.findAll("span", { "class" : "meta_num" })[0].string
  17.         view = view.strip().replace(",","")
  18.         if  view[-1] == 'k':
  19.             view = view[:-1]
  20.             view = int(float(view) * 1000)
  21.         elif view[-1] == 'm':
  22.             view = view[:-1]
  23.             view = int(float(view) * 1000000)
  24.     except Exception as e:
  25.         #print e
  26.         view = 0
  27.     view = int(str(view))
  28.     print str(view) + " views"
  29.  
  30.  
  31.     try:
  32.         upvote = div.findAll("a", { "class" : "VoterListModalLink" })[0].string
  33.         upvote = upvote.strip().replace(",","")
  34.         upvote = upvote.split(' ')[0]
  35.  
  36.         upvote = upvote.strip()
  37.         if  upvote[-1] == 'k':
  38.             upvote = upvote[:-1]
  39.             upvote = int(float(upvote) * 1000)
  40.         elif upvote[-1] == 'm':
  41.             upvote = upvote[:-1]
  42.             upvote = int(float(upvote) * 1000000)
  43.     except Exception as e:
  44.         #print e
  45.         upvote = 0
  46.     upvote = int(str(upvote))
  47.     print str(upvote) + " upvotes"
  48.  
  49.     if upvote == 0 :
  50.         print "No upvotes."
  51.         viewPerUpvote = 100000000000000
  52.     else:
  53.         viewPerUpvote = float(view)/float(upvote)
  54.         print str(viewPerUpvote) + " views per upvote ===============>\n\n"
  55.  
  56.     lisht = [user, view, upvote, viewPerUpvote]
  57.     howAmI.append(lisht)
  58.  
  59. for l in howAmI:
  60.     print l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement