Nokogiri XML 和循环不工作
Nokogiri XML and loop not working
我制作了一个脚本来从 indeed.com API 获取数据。我收到回复,但 returns 每个公司名称在前,然后是每个职位。
我现在得到的:
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
我制作了一个脚本来从 indeed.com API 获取数据。我收到回复,但 returns 每个公司名称在前,然后是每个职位。
我现在得到的:
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