Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import pandas as pd
- import xml.etree.ElementTree as et
- # Счетчик тегов
- GLOBAL_COUNTER = {}
- # Извлечение структуры
- def get_structure(parent):
- if not len(parent):
- return {}
- structure = {}
- for child in parent:
- if child.tag != "vacancy":
- # Обновляем счетчик тегов
- count = len([e.tag for e in parent if e.tag == child.tag])
- GLOBAL_COUNTER[child.tag] = max(GLOBAL_COUNTER[child.tag], count) if child.tag in GLOBAL_COUNTER else 1
- structure[child.tag] = deep_merge(get_structure(child), structure[child.tag]) \
- if child.tag in structure else \
- get_structure(child)
- return structure
- # Объединение списков
- def deep_merge(source, destination):
- for key, value in source.items():
- if isinstance(value, dict):
- node = destination.setdefault(key, {})
- deep_merge(value, node)
- else:
- destination[key] = value
- return destination
- if __name__ == "__main__":
- filename = "resources/obv_xml_204"
- xml = et.parse(filename)
- source = xml.getroot() # source
- vacancies = source[0] # vacancies
- # print(get_structure(vacancies))
- # print(GLOBAL_COUNTER)
- columns = [
- "url",
- "mobile-url",
- "creation_date",
- "update-date",
- "min-salary",
- "max-salary",
- "currency",
- "industry",
- "job-name",
- "employment",
- "schedule",
- "description",
- "duty",
- "term",
- "education",
- "qualification",
- "experience",
- "location",
- "latitude",
- "longitude",
- "metro-stations",
- "company-name",
- "company-contact-name",
- "company-hr-agency",
- "company-phone",
- "company-description",
- "company-logo",
- "company-site",
- "company-email"
- ]
- def find(element, tag, return_text=True):
- tag = element.find(tag)
- if tag is not None:
- tag = tag.text if return_text else tag
- if isinstance(tag, str):
- tag = tag.replace(',', ';')
- return tag
- processed = []
- for vacancy in vacancies:
- url = find(vacancy, "url")
- mobile_url = find(vacancy, "mobile-url")
- creation_date = find(vacancy, "creation-date")
- update_date = find(vacancy, "update-date")
- currency = find(vacancy, "currency")
- employment = find(vacancy, "employment")
- schedule = find(vacancy, "schedule")
- description = find(vacancy, "description")
- duty = find(vacancy, "duty")
- job_name = find(vacancy, "job-name")
- salary = find(vacancy, "salary")
- min_salary = max_salary = None
- if salary and isinstance(salary, str):
- tokens = salary.lower().split()
- try:
- min_salary = tokens[tokens.index("от") + 1]
- except Exception:
- pass
- try:
- max_salary = tokens[tokens.index("до") + 1]
- except Exception:
- pass
- industry = find(vacancy, "category", return_text=False)
- if industry and len(industry):
- industry = find(industry, "industry")
- term = find(vacancy, "term", return_text=False)
- if term and len(term):
- term = find(term, "text")
- requirement = find(vacancy, "requirement", return_text=False)
- education = qualification = experience = None
- if requirement and len(requirement):
- education = find(requirement, "education")
- qualification = find(requirement, "qualification")
- experience = find(requirement, "experience")
- addresses = find(vacancy, "addresses", return_text=False)
- location = longitude = latitude = metro_stations = None
- if addresses and len(addresses):
- address = find(addresses, "address", return_text=False)
- if address and len(address):
- location = find(address, "location")
- longitude = find(address, "lng")
- latitude = find(address, "lat")
- metro_stations = [metro.text for metro in address.iter("metro")]
- if metro_stations and isinstance(metro_stations, list) and len(metro_stations):
- metro_stations = ", ".join(metro_stations)
- if not metro_stations:
- metro_stations = None
- company = find(vacancy, "company", return_text=False)
- company_name = contact_name = hr_agency = company_desc = logo = site = phone = emails = None
- if company and len(company):
- company_name = find(company, "name")
- contact_name = find(company, "contact-name")
- hr_agency = find(company, "hr-agency")
- company_desc = find(company, "description")
- logo = find(company, "logo")
- site = find(company, "site")
- phone = find(company, "phone")
- emails = [email.text for email in company.iter("email")]
- if emails and isinstance(emails, list) and len(emails) > 0:
- emails_modified = []
- for email in emails:
- email = str(email)
- groups = re.findall(
- r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:"
- r"[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-"
- r"\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*"
- r"[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:"
- 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-"
- r"\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])",
- email)
- if groups:
- emails_modified.append(email)
- elif email != '-' and bool(re.compile(r"[0-9()\-\s]").search(str(email))):
- if not phone:
- phone = []
- if isinstance(phone, str) or isinstance(phone, int):
- phone = [str(phone)]
- phone.append(email)
- if isinstance(phone, list):
- phone = ", ".join(map(lambda x: str(x), phone))
- emails = ", ".join(emails_modified)
- if not emails:
- emails = None
- processed.append([url, mobile_url, creation_date, update_date, min_salary, max_salary, currency, industry,
- job_name, employment, schedule, description, duty, term, education, qualification, experience,
- location, latitude, longitude, metro_stations, company_name, contact_name, hr_agency, phone,
- company_desc, logo, site, emails])
- dfp = pd.DataFrame(processed, columns=columns)
- dfp.to_csv("df.csv", index=False)
Add Comment
Please, Sign In to add comment