public
Created

Crawling 43612 XML files

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'rexml/document'
require 'csv'
include REXML
@urls = Array.new
@ID = Array.new
@titleSv = Array.new
@titleEn = Array.new
@identifier = Array.new
@typeOfLevel = Array.new
@typeOfResponsibleBody = Array.new
@courseTyp = Array.new
@credits = Array.new
@degree = Array.new
@preAcademic = Array.new
@subjectCodeVhs = Array.new
@descriptionSv = Array.new
@visibleToSweApplicants = Array.new
@lastedited = Array.new
@expires = Array.new
 
 
# Hämtar alla XML-länkar
htmldoc = Nokogiri::HTML(open('http://testnavet.skolverket.se/SusaNavExport/EmilExporter?GetEvent&EMILVersion=1.1&NotExpired&EEFormOfStudy=normal&EIAcademicType=UoH&SelectEI'))
# Hämtar alla länkar för xml-filerna och sparar dom i arrayn urls
htmldoc.xpath('//a/@href').each do |links|
@urls << links.content
end
 
@urls.each do |url|
# Loop throw the XML files and grab element nodes
xmldoc = REXML::Document.new(open(url).read)
# Root element
root = xmldoc.root
# Hämtar info-id
@ID << root.attributes["id"]
# TitleSv
xmldoc.elements.each("/educationInfo/titles/title[1] | /ns:educationInfo/ns:titles/ns:title[1]"){
|e| m = e.text
m = m.to_s
next if m.empty?
@titleSv << m
}
# TitleEn
xmldoc.elements.each("/educationInfo/titles/title[2] | /ns:educationInfo/ns:titles/ns:title[2]"){
|e| m = e.text
m = m.to_s
next if m.empty?
@titleSv << m
}
# Identifier
xmldoc.elements.each("/educationInfo/identifier | /ns:educationInfo/ns:identifier"){
|e| m = e.text
m = m.to_s
next if m.empty?
@identifier << m
}
# typeOfLevel
xmldoc.elements.each("/educationInfo/educationLevelDetails/typeOfLevel | /ns:educationInfo/ns:educationLevelDetails/ns:typeOfLevel"){
|e| m = e.text
m = m.to_s
next if m.empty?
@typeOfLevel << m
}
# typeOfResponsibleBody
xmldoc.elements.each("/educationInfo/educationLevelDetails/typeOfResponsibleBody | /ns:educationInfo/ns:educationLevelDetails/ns:typeOfResponsibleBody"){
|e| m = e.text
m = m.to_s
next if m.empty?
@typeOfResponsibleBody << m
}
# courseTyp
xmldoc.elements.each("/educationInfo/educationLevelDetails/academic/course/type | /ns:educationInfo/ns:educationLevelDetails/ns:academic/ns:courseOfferingPackage/ns:type"){
|e| m = e.text
m = m.to_s
next if m.empty?
@courseTyp << m
}
# credits
xmldoc.elements.each("/educationInfo/credits/exact | /ns:educationInfo/ns:credits/ns:exact"){
|e| m = e.text
m = m.to_s
next if m.empty?
@credits << m
}
# degree
xmldoc.elements.each("/educationInfo/degrees/degree | /ns:educationInfo/ns:degrees/ns:degree"){
|e| m = e.text
m = m.to_s
next if m.empty?
@degree << m
}
# @preAcademic
xmldoc.elements.each("/educationInfo/prerequisites/academic | /ns:educationInfo/ns:prerequisites/ns:academic"){
|e| m = e.text
m = m.to_s
next if m.empty?
@preAcademic << m
}
# @subjectCodeVhs
xmldoc.elements.each("/educationInfo/subjects/subject/code[1] | /ns:educationInfo/ns:subjects/ns:subject/ns:code"){
|e| m = e.text
m = m.to_s
next if m.empty?
@subjectCodeVhs << m
}
# DescriptionSv
xmldoc.elements.each("/educationInfo/extensionInfo/nya:textualDescription/nya:textualDescriptionPhrase | /ns:educationInfo/ns:extensionInfo/gu:guInfoExtensions/gu:guSubject/gu:descriptions/gu:description | //*[name()='ct:text']"){
|e| m = e.text
m = m.to_s
m.gsub!(/<.+?>/, "")
next if m.empty?
@descriptionSv << m
}
# Hämtar dokuments utgångs-datum
@expires << root.attributes["expires"]
# Hämtar dokuments lastedited
@lastedited << root.attributes["lastEdited"]
 
# Lagrar dom i uni.CSV
CSV.open("eduction_normal.csv", "wb") do |row|
(0..@ID.length - 1).each do |index|
row << [@ID[index], @titleSv[index], @titleEn[index], @identifier[index], @typeOfLevel[index], @typeOfResponsibleBody[index], @courseTyp[index], @credits[index], @degree[index], @preAcademic[index], @subjectCodeVhs[index], @descriptionSv[index], @lastedited[index], @expires[index]]
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.