Advertisement
FiddleComputers

babel_generate

Mar 12th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import urllib.request
  2. import random
  3. from bs4 import BeautifulSoup
  4.  
  5. output = open("output_babel.txt", "r+")
  6. lines = 1
  7. reqNumber = 1
  8.  
  9. while output.readline():
  10.     if(lines % 48 == 0):
  11.         reqNumber += 1
  12.  
  13.     lines += 1
  14.  
  15. while True:
  16.     keyLength = random.randint(1, 3260)
  17.     key = ""
  18.  
  19.     for i in range(0, keyLength):
  20.         randAscii = random.randint(87, 122)
  21.         if(randAscii < 97):
  22.             randAscii %= 10
  23.             key += str(randAscii)
  24.         else:
  25.             key += chr(randAscii)
  26.  
  27.     wall = "-w" + str(random.randint(1, 4))
  28.  
  29.     shelf = "-s" + str(random.randint(1, 5))
  30.  
  31.     volume = random.randint(1, 32)
  32.     if volume < 10:
  33.         volume = "-v0" + str(volume)
  34.     else:
  35.         volume = "-v" + str(volume)
  36.  
  37.     page = ":" + str(random.randint(1, 410))
  38.  
  39.     url = "https://libraryofbabel.info/book.cgi?"+key+wall+shelf+volume+page
  40.  
  41.     html_page = BeautifulSoup(urllib.request.urlopen(url), "html.parser")
  42.  
  43.     title = html_page.title.get_text()
  44.     text = html_page.find( "div", {"class":"bookrealign"}).get_text()
  45.  
  46.     print("#### "+str(reqNumber)+" ####")
  47.     print("Key: "+key)
  48.     print("Title: "+title)
  49.     print("Wall: "+str(wall[2]))
  50.     print("Shelf: "+str(shelf[2]))
  51.     print("Volume: "+str(volume[2])+str(volume[3]))
  52.     print(text)
  53.  
  54.     outputText  = "#### "+str(reqNumber)+" ####"
  55.     outputText += "\nKey: "+key
  56.     outputText += "\nTitle: "+title
  57.     outputText += "\nWall: "+str(wall[2])
  58.     outputText += "\nShelf: "+str(shelf[2])
  59.     outputText += "\nVolume: "+str(volume[2])+str(volume[3])
  60.     outputText += "\n" + text
  61.  
  62.     if(reqNumber > 1):
  63.         output.write("\n\n")
  64.  
  65.     output.write(outputText)
  66.  
  67.     reqNumber += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement