alexander0803

Untitled

Oct 15th, 2020
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. import re
  2. import pandas as pd
  3. import xml.etree.ElementTree as et
  4.  
  5. # Счетчик тегов
  6. GLOBAL_COUNTER = {}
  7.  
  8.  
  9. # Извлечение структуры
  10. def get_structure(parent):
  11. if not len(parent):
  12. return {}
  13.  
  14. structure = {}
  15. for child in parent:
  16. if child.tag != "vacancy":
  17. # Обновляем счетчик тегов
  18. count = len([e.tag for e in parent if e.tag == child.tag])
  19. GLOBAL_COUNTER[child.tag] = max(GLOBAL_COUNTER[child.tag], count) if child.tag in GLOBAL_COUNTER else 1
  20. structure[child.tag] = deep_merge(get_structure(child), structure[child.tag]) \
  21. if child.tag in structure else \
  22. get_structure(child)
  23.  
  24. return structure
  25.  
  26.  
  27. # Объединение списков
  28. def deep_merge(source, destination):
  29. for key, value in source.items():
  30. if isinstance(value, dict):
  31. node = destination.setdefault(key, {})
  32. deep_merge(value, node)
  33. else:
  34. destination[key] = value
  35. return destination
  36.  
  37.  
  38. if __name__ == "__main__":
  39. filename = "resources/obv_xml_204"
  40.  
  41. xml = et.parse(filename)
  42.  
  43. source = xml.getroot() # source
  44. vacancies = source[0] # vacancies
  45.  
  46. # print(get_structure(vacancies))
  47. # print(GLOBAL_COUNTER)
  48.  
  49. columns = [
  50. "url",
  51. "mobile-url",
  52. "creation_date",
  53. "update-date",
  54. "min-salary",
  55. "max-salary",
  56. "currency",
  57. "industry",
  58. "job-name",
  59. "employment",
  60. "schedule",
  61. "description",
  62. "duty",
  63. "term",
  64. "education",
  65. "qualification",
  66. "experience",
  67. "location",
  68. "latitude",
  69. "longitude",
  70. "metro-stations",
  71. "company-name",
  72. "company-contact-name",
  73. "company-hr-agency",
  74. "company-phone",
  75. "company-description",
  76. "company-logo",
  77. "company-site",
  78. "company-email"
  79. ]
  80.  
  81. def find(element, tag, return_text=True):
  82. tag = element.find(tag)
  83. if tag is not None:
  84. tag = tag.text if return_text else tag
  85. if isinstance(tag, str):
  86. tag = tag.replace(',', ';')
  87. return tag
  88.  
  89. processed = []
  90. for vacancy in vacancies:
  91. url = find(vacancy, "url")
  92. mobile_url = find(vacancy, "mobile-url")
  93. creation_date = find(vacancy, "creation-date")
  94. update_date = find(vacancy, "update-date")
  95. currency = find(vacancy, "currency")
  96. employment = find(vacancy, "employment")
  97. schedule = find(vacancy, "schedule")
  98. description = find(vacancy, "description")
  99. duty = find(vacancy, "duty")
  100. job_name = find(vacancy, "job-name")
  101.  
  102. salary = find(vacancy, "salary")
  103. min_salary = max_salary = None
  104. if salary and isinstance(salary, str):
  105. tokens = salary.lower().split()
  106.  
  107. try:
  108. min_salary = tokens[tokens.index("от") + 1]
  109. except Exception:
  110. pass
  111.  
  112. try:
  113. max_salary = tokens[tokens.index("до") + 1]
  114. except Exception:
  115. pass
  116.  
  117. industry = find(vacancy, "category", return_text=False)
  118. if industry and len(industry):
  119. industry = find(industry, "industry")
  120.  
  121. term = find(vacancy, "term", return_text=False)
  122. if term and len(term):
  123. term = find(term, "text")
  124.  
  125. requirement = find(vacancy, "requirement", return_text=False)
  126. education = qualification = experience = None
  127. if requirement and len(requirement):
  128. education = find(requirement, "education")
  129. qualification = find(requirement, "qualification")
  130. experience = find(requirement, "experience")
  131.  
  132. addresses = find(vacancy, "addresses", return_text=False)
  133. location = longitude = latitude = metro_stations = None
  134. if addresses and len(addresses):
  135. address = find(addresses, "address", return_text=False)
  136. if address and len(address):
  137. location = find(address, "location")
  138. longitude = find(address, "lng")
  139. latitude = find(address, "lat")
  140. metro_stations = [metro.text for metro in address.iter("metro")]
  141. if metro_stations and isinstance(metro_stations, list) and len(metro_stations):
  142. metro_stations = ", ".join(metro_stations)
  143. if not metro_stations:
  144. metro_stations = None
  145.  
  146. company = find(vacancy, "company", return_text=False)
  147. company_name = contact_name = hr_agency = company_desc = logo = site = phone = emails = None
  148. if company and len(company):
  149. company_name = find(company, "name")
  150. contact_name = find(company, "contact-name")
  151. hr_agency = find(company, "hr-agency")
  152. company_desc = find(company, "description")
  153. logo = find(company, "logo")
  154. site = find(company, "site")
  155. phone = find(company, "phone")
  156. emails = [email.text for email in company.iter("email")]
  157. if emails and isinstance(emails, list) and len(emails) > 0:
  158. emails_modified = []
  159. for email in emails:
  160. email = str(email)
  161. groups = re.findall(
  162. r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:"
  163. r"[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-"
  164. r"\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*"
  165. r"[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:"
  166. r"(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-"
  167. r"\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])",
  168. email)
  169. if groups:
  170. emails_modified.append(email)
  171. elif email != '-' and bool(re.compile(r"[0-9()\-\s]").search(str(email))):
  172. if not phone:
  173. phone = []
  174. if isinstance(phone, str) or isinstance(phone, int):
  175. phone = [str(phone)]
  176. phone.append(email)
  177. if isinstance(phone, list):
  178. phone = ", ".join(map(lambda x: str(x), phone))
  179. emails = ", ".join(emails_modified)
  180. if not emails:
  181. emails = None
  182.  
  183. processed.append([url, mobile_url, creation_date, update_date, min_salary, max_salary, currency, industry,
  184. job_name, employment, schedule, description, duty, term, education, qualification, experience,
  185. location, latitude, longitude, metro_stations, company_name, contact_name, hr_agency, phone,
  186. company_desc, logo, site, emails])
  187.  
  188. dfp = pd.DataFrame(processed, columns=columns)
  189. dfp.to_csv("df.csv", index=False)
  190.  
Add Comment
Please, Sign In to add comment