collinsanele

update

Sep 3rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. from lxml import html
  2. from bs4 import BeautifulSoup
  3. import json
  4.  
  5.  
  6. """ NB: for only 5397124106352075058.htm and 1259815981046145990.htm """
  7.  
  8.  
  9.  
  10. def html2json(path):
  11. if "5397124106352075058" in path:
  12. results = []
  13. container = []
  14. with open(path) as f:
  15. content = f.read()
  16.  
  17. soup = BeautifulSoup(content, "lxml")
  18. paras = soup.findAll("p", {"align":"JUSTIFY"})
  19. indexes = []
  20. for i,item in enumerate(paras):
  21. if "Sol" in item.text:
  22. indexes.append(i)
  23.  
  24. for i,item in enumerate(indexes):
  25. try:
  26. container.append(paras[indexes[i]:indexes[i+1]])
  27. except IndexError:
  28. pass
  29.  
  30.  
  31. qnums = []
  32. c_options = []
  33. str_container = []
  34.  
  35. for i, item in enumerate(container):
  36. if not item[0].findAll("b"):
  37. del container[i]
  38.  
  39.  
  40. for item in container:
  41. try:
  42. target = item[0].findAll("b")
  43. except Exception as e:
  44. print(e)
  45. pass
  46.  
  47. try:
  48. qn = target[0].text.split()[0].strip()
  49. qnums.append(qn)
  50. except Exception:
  51. pass
  52.  
  53. try:
  54. if len(target) == 2:
  55. c_option = target[1].text
  56. c_options.append(c_option.strip())
  57. elif len(target) == 1:
  58. c_option = target[0].text.split()[1]
  59. c_options.append(c_option.strip())
  60.  
  61. except IndexError:
  62. c_option = " "
  63. c_options.append(c_option)
  64. pass
  65.  
  66.  
  67. #container
  68. for item in container:
  69. try:
  70. trash = item[0].find("b").decompose()
  71. except:
  72. pass
  73.  
  74.  
  75. str_container = [str(item).replace("[", "").replace('<p align="JUSTIFY">', "")
  76. .replace("</p>", "").replace(",", "")
  77. .replace("]", "").replace("IND", "NEL").strip() for item in container]
  78.  
  79. for x in range(len(qnums)):
  80. obj = {"Question Number":"", "Correct Option":"", "Solution":""}
  81. obj["Question Number"] = qnums[x]
  82. obj["Correct Option"] = c_options[x]
  83. obj["Solution"] = str_container[x]
  84.  
  85. results.append(obj)
  86.  
  87. final = {"results":results}
  88.  
  89.  
  90. #Saves result as json in the current working directory
  91. with open("539.json", "w") as f:
  92. json.dump(final, f)
  93. print("Done!")
  94.  
  95.  
  96. elif "1259815981046145990" in path:
  97. qnums = []
  98. c_options = []
  99. results = []
  100. solutions = []
  101. container = []
  102. str_container = []
  103.  
  104. with open(path) as f:
  105. content = f.read()
  106.  
  107. soup = BeautifulSoup(content, "lxml")
  108. paras = soup.findAll("p", {"align":"JUSTIFY"})
  109. indexes = []
  110. for i,item in enumerate(paras):
  111. if "Sol" in item.text:
  112. indexes.append(i)
  113.  
  114. for i,item in enumerate(indexes):
  115. try:
  116. container.append(paras[indexes[i]:indexes[i+1]])
  117. except IndexError:
  118. pass
  119.  
  120. container1 = []
  121.  
  122. for item in container:
  123. for sub in item:
  124. if "Ans" in sub.text:
  125. container1.append(item)
  126.  
  127.  
  128. for item in container1:
  129. for sub in item:
  130. if "Sol" in sub.text:
  131. qn = sub.text.split()[0]
  132. qnums.append(qn)
  133.  
  134. if "Ans" in sub.text:
  135. c_option = sub.text
  136. c_options.append(c_option)
  137.  
  138.  
  139.  
  140. c_options = [item.replace("Ans.", "").replace("Ans", "").strip() for item in c_options]
  141.  
  142.  
  143. str_container = [str(item).replace("[", "").replace('<p align="JUSTIFY">', "")
  144. .replace("</p>", "").replace(",", "")
  145. .replace("]", "").replace("IND", "NEL").strip() for item in container1]
  146.  
  147.  
  148. str_container = [item.replace("<b>Ans. (A)", "")
  149. .replace("<b>Ans. (B)", "")
  150. .replace("<b>Ans. (C)", "").replace("<b>Ans. (D)", "")
  151. .replace("<b>", "").replace("</b>", "").strip() for item in str_container]
  152.  
  153.  
  154.  
  155. for x in range(len(qnums)):
  156. obj = {"Question Number":"", "Correct Option":"", "Solution":""}
  157. obj["Question Number"] = qnums[x]
  158. obj["Correct Option"] = c_options[x]
  159. obj["Solution"] = str_container[x]
  160.  
  161. results.append(obj)
  162.  
  163. final = {"results":results}
  164.  
  165. #Saves result as json in the current working directory
  166. with open("1259.json", "w") as f:
  167. json.dump(final, f)
  168. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment