Nokogiri XML和循环不起作用



我编写了一个脚本从indeed.com API获取数据。我得到一个响应,但它首先返回每个公司名称,然后返回一个块中的每个职位名称。

我现在得到的:

ecutive KingswoodSales ExecutiveTrainee Van Sales ProfessionalJunior Broker (Opener) - Hong Kong - flights & accomodationTrainee Sales ExecutiveSales AssociateSales Executive
Mercedes-BenzMercedes-BenzMercedes-BenzSwinton GroupPorscheMercedes-BenzElite Sales RecruitmentImperial TobaccoThreeMercedes-Benz

我尝试添加每个do循环,但现在它是空白的,我没有得到一个错误。

我的脚本:

require 'nokogiri'
require 'open-uri'
xmls = Nokogiri::XML(open('http://api.indeed.com/ads/apisearch?publisher=apikey&q=sales&l=&sort=&radius=40&st=&jt=&start=&limit=&fromage=&filter=&co=uk&v=2')) 
xmls.each do |xml|    
  jobtitle = xml.search('jobtitle').text
  company  = xml.search('company').text
  puts jobtitle
  puts company 
end

我的目标是得到这样的数据:

Company 1
company name
company jobtitle
Company 2
company name
company jobtitle
Company 3
company name
company jobtitle 
...

这是XML响应:

<?xml version="1.0" encoding="UTF-8" ?>
<response version="2">
    <query>java</query>
    <location>austin, tx</location>
    <dupefilter>true</dupefilter>
    <highlight>false</highlight>
    <totalresults>547</totalresults>
    <start>1</start>
    <end>10</end>
    <radius>25</radius>
    <pageNumber>0</pageNumber>
    <results>
        <result>
            <jobtitle>Java Developer</jobtitle>
            <company>XYZ Corp.</company>
            <city>Austin</city>
            <state>TX</state>
            <country>US</country>
            <formattedLocation>Austin, TX</formattedLocation>
            <source>Dice</source>
            <date>Mon, 02 Aug 2010 16:21:00 GMT</date>
            <snippet>looking for an object-oriented Java Developer... Java Servlets, HTML, JavaScript,
            AJAX, Struts, Struts2, JSF) desirable. Familiarity with Tomcat and the Java...</snippet>
            <url>http://www.indeed.com/viewjob?jk=12345&indpubnum=8343699265155203</url>
            <onmousedown>indeed_clk(this,'0000');</onmousedown>
            <latitude>30.27127</latitude>
            <longitude>-97.74103</longitude>
            <jobkey>12345</jobkey>
            <sponsored>false</sponsored>
            <expired>false</expired>
            <formattedLocationFull>Austin, TX</formattedLocationFull>
            <formattedRelativeTime>11 hours ago</formattedRelativeTime>
        </result>
        ...
    </results>
</response>

你可以使用CSS选择器。

xmls = Nokogiri::XML(open('......')) 
xmls.css('results result').each do |xml|
  jobtitle = xml.css('jobtitle').text
  company = xml.css('company').text
  puts jobtitle
  puts company 
end

相关内容

  • 没有找到相关文章

最新更新