collinsanele

Scholarship_scraper.py on ec2

Jul 26th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1.  
  2. import json
  3. import string
  4. import time
  5. from tqdm import tqdm
  6. import ast
  7.  
  8. from selenium import webdriver
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.chrome.options import Options
  13. from pyvirtualdisplay import Display
  14.  
  15.  
  16. #Important
  17. display = Display(visible=0, size=(800, 600))
  18. display.start()
  19.  
  20.  
  21. options = Options()
  22. options.add_argument("--start-maximized")
  23. options.add_argument("--headless")
  24. options.add_argument('--no-sandbox')
  25. options.add_argument('--disable-dev-shm-usage')
  26.  
  27.  
  28. print("starting")
  29.  
  30. driver=webdriver.Chrome(chrome_options=options)
  31.  
  32.  
  33. with open("milestone_2.txt", encoding='cp1252') as f:
  34. names_and_links = ast.literal_eval(f.read())
  35.  
  36.  
  37.  
  38.  
  39. result = []
  40.  
  41. for item in tqdm(names_and_links[0:]):
  42. obj = {}
  43. name = ""
  44. sponsor = ""
  45. data = {}
  46. required_materials_list = []
  47.  
  48. obj["Eligibility Requirements"] = {}
  49. obj["Qualified Institutions & Level of Study"] = {}
  50. obj["Application Requirements"] = {}
  51. obj["General Information"] = {}
  52. obj["Deadlines & Fees"] = {}
  53. obj["Contact Information"] = {}
  54. obj["Url"] = item["Link"]
  55.  
  56.  
  57. driver.get(item["Link"])
  58. driver.implicitly_wait(60)
  59. time.sleep(4.3)
  60.  
  61.  
  62. try:
  63. name = driver.find_element_by_class_name("scholarshipDetails").find_element_by_tag_name("div").text.split("\n")[1].strip()
  64.  
  65. except:
  66. pass
  67.  
  68.  
  69. try:
  70. sponsor = driver.find_element_by_class_name("scholarshipDetails").find_element_by_class_name("copySm").find_element_by_class_name("right").text.strip()
  71.  
  72. except Exception as e:
  73. pass
  74.  
  75.  
  76.  
  77. try:
  78. eligibility_requirements = driver.find_element_by_class_name("eligibilityView").find_elements_by_class_name("searchResultField")
  79.  
  80.  
  81. for item in eligibility_requirements:
  82. if ":" in item.text:
  83. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  84. value = ", ".join(item.text.split(":")[1].strip().split("\n"))
  85. data = {key:value}
  86. obj["Eligibility Requirements"].update(data)
  87.  
  88.  
  89. except Exception as e:
  90. pass
  91.  
  92.  
  93.  
  94. try:
  95. qualified_institutions = driver.find_element_by_class_name("qualifiedInstitutionsView").find_elements_by_class_name("searchResultField")
  96.  
  97. for item in qualified_institutions:
  98. if ":" in item.text:
  99. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  100. value = " ".join(item.text.split(":")[1:]).strip()
  101.  
  102. if "Award may be used for" in key or "Award may be used at" in key:
  103. data = {key:value.split("\n")}
  104. obj["Qualified Institutions & Level of Study"].update(data)
  105.  
  106. else:
  107. data = {key: ", ".join(value.split("\n"))}
  108. obj["Qualified Institutions & Level of Study"].update(data)
  109.  
  110.  
  111. except Exception as e:
  112. pass
  113.  
  114.  
  115.  
  116.  
  117.  
  118. try:
  119. app_requirements = driver.find_element_by_class_name("applicationRequirementsView").find_elements_by_class_name("searchResultField")
  120.  
  121.  
  122. for item in app_requirements:
  123. if ":" in item.text:
  124. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  125. value = item.text.split(":")[1].strip().split("\n")
  126. data = {key:value}
  127.  
  128. obj["Application Requirements"].update(data)
  129.  
  130. except Exception as e:
  131. pass
  132.  
  133.  
  134.  
  135.  
  136. try:
  137. required_materials = driver.find_element_by_class_name("applicationRequirementsView").find_element_by_class_name("searchResultField").text.strip().split("\n")
  138.  
  139. for req_mat in required_materials:
  140. required_materials_list.append(req_mat.strip())
  141.  
  142. obj["Application Requirements"].update({"Required Materials": required_materials_list})
  143.  
  144. except:
  145. pass
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. try:
  157. gen_info = driver.find_element_by_class_name("scholarshipProfileGeneralInformationView").find_elements_by_class_name("searchResultField")
  158.  
  159.  
  160. for item in gen_info:
  161. if ":" in item.text:
  162. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  163. value = " ".join(item.text.split(":")[1:]).strip()
  164.  
  165. if "Type of award" in key:
  166. data = {key:value.split("\n")}
  167. obj["General Information"].update(data)
  168.  
  169. else:
  170. data = {key: ", ".join(value.split("\n"))}
  171. obj["General Information"].update(data)
  172.  
  173.  
  174. except Exception as e:
  175. pass
  176.  
  177.  
  178.  
  179.  
  180. try:
  181. deadlines = driver.find_element_by_class_name("scholarshipProfileDeadlinesView").find_elements_by_class_name("searchResultField")
  182.  
  183.  
  184. for item in deadlines:
  185. if ":" in item.text:
  186. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  187. value = " ".join(item.text.split(":")[1:]).strip()
  188.  
  189. data = {key: ", ".join(value.split("\n"))}
  190. obj["Deadlines & Fees"].update(data)
  191.  
  192.  
  193. except Exception as e:
  194. pass
  195.  
  196.  
  197.  
  198. try:
  199. contact_info = driver.find_element_by_class_name("scholarshipProfileContactInformationView").find_elements_by_class_name("searchResultField")
  200.  
  201.  
  202. for item in contact_info:
  203. if ":" in item.text:
  204. key = ", ".join(item.text.split(":")[0].strip().split("\n"))
  205. value = " ".join(item.text.split(":")[1:]).strip()
  206.  
  207. data = {key: ", ".join(value.split("\n"))}
  208. obj["Contact Information"].update(data)
  209.  
  210.  
  211. except Exception as e:
  212. pass
  213.  
  214.  
  215.  
  216.  
  217.  
  218. obj["Name"] = name
  219.  
  220. obj["Sponsor"] = sponsor
  221.  
  222.  
  223. result.append(obj)
  224.  
  225.  
  226.  
  227.  
  228. with open("result.json", "w", encoding='cp1252') as f:
  229. json.dump(result, f, indent=4)
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
Advertisement
Add Comment
Please, Sign In to add comment