2012-04-04 2 views
1

기계 태그 + nokogiri로 스크립트 태그 내의 값을 파싱하려고합니다. 이것은 내가 할 수있는 한도입니다nokogiri를 사용하여 javascript 함수 요소 구문 분석

1.9.3-p125 :107 > agent.page.search("script")[7] 
=> #<Nokogiri::XML::Element:0x3ff1e10f3ff8 name="script" attributes= 
[#<Nokogiri::XML::Attr:0x3ff1e10f3f80 name="type" value="text/javascript">] children= 
[#<Nokogiri::XML::CDATA:0x3ff1e10f39b8 "countdownFactory.create('47884', '1333724400000', '');countdownFactory.create('48436', '1333638000000', '');countdownFactory.create('46085', '1333627200000', '');countdownFactory.create('48151', '1333551600000', '');countdownFactory.create('48211', '1333638000000', '');countdownFactory.create('48511', '1333551600000', '');countdownFactory.create('48513', '1333551600000', '');countdownFactory.create('48482', '1333551600000', '');countdownFactory.create('48439', '1333551600000', '');countdownFactory.create('48299', '1333551600000', '');countdownFactory.create('48272', '1333670400000', '');countdownFactory.create('46371', '1333638000000', '');countdownFactory.create('48254', '1333724400000', '');countdownFactory.create('46086', '1333638000000', '');countdownFactory.create('48317', '1333638000000', '');countdownFactory.create('48435', '1333724400000', '');countdownFactory.create('47223', '1334059200000', '');countdownFactory.create('48234', '1333972800000', '');countdownFactory.create('48407', '1333638000000', '');countdownFactory.create('48429', '1333638000000', '');countdownFactory.create('48212', '1333638000000', '');countdownFactory.create('48275', '1333551600000', '');countdownFactory.create('48205', '1333551600000', '');countdownFactory.create('48414', '1333886400000', '');countdownFactory.create('48185', '1333713600000', '');countdownFactory.create('48215', '1333540800000', '');countdownFactory.create('47636', '1333638000000', '');">] 

어떻게 모든 countdownFactory.create 요소를 해시로 가져 옵니까? 감사!

답변

6

노코 기리 자바 스크립트 구문 분석을하지 않습니다, 그러나 이것은 정규 표현식 구문 분석을 너무 열심히되지 않습니다 :

element = agent.page.search("script")[7] 
text = element.text # not 100% sure on this line. Just need the script text though. 
Hash[text.scan(/countdownFactory.create\('(\d+)', '(\d+)', ''\)/)] 
+0

덕분에, 난 그냥 정규식과 구문 분석 결국 않았다. 지금은 충분히 잘 작동합니다. :) – LeoAlmighty

관련 문제