我有以下代码,它可以正常工作: troveUrlhttp://trove.nla.gov.au/work/23043869/troveUrl 但是我希望从同一页面获得以下’id’之后的值,并且无法得到它! work id="23043869" url="/work/23043869" 这是我目前
<troveUrl>http://trove.nla.gov.au/work/23043869</troveUrl>
但是我希望从同一页面获得以下’id’之后的值,并且无法得到它!
<work id="23043869" url="/work/23043869">
这是我目前拥有的代码
var request = require ('request'), cheerio = require ('cheerio'); request('http://api.trove.nla.gov.au/result?key=6k6oagt6ott4ohno&zone=book&q-year1-date=2000&l-advformat=Thesis&l-australian=y&q-term2=&q-term3=&q-term0=&q-field1=title%3A&q-type2=all&q-field0=&q-term1=&q-type3=all&q-field3=subject%3A&q-type0=all&q-field2=creator%3A&q-type1=all&l-availability=y%2Ff&q=+date%3A[2000+TO+2014]&q-year2-date=2014&n=1', function (error, response, html) { if (!error && response.statusCode == 200) { var $= cheerio.load(html); $('troveurl').each(function(i, element){ var id = $(this); console.log(id.text()); }); } });
任何协助赞赏.
您应该在options对象中传递xmlMode:true,然后您可以将其解析为XML.然后,您可以使用$(‘tag’).attr(‘attribute’)和$(‘tag’).text()来获取标签和数据,以便在您完成之后获取标签之间的数据.
var request = require('request'), cheerio = require('cheerio'); request('http://api.trove.nla.gov.au/result?key=6k6oagt6ott4ohno&zone=book&q-year1-date=2000&l-advformat=Thesis&l-australian=y&q-term2=&q-term3=&q-term0=&q-field1=title%3A&q-type2=all&q-field0=&q-term1=&q-type3=all&q-field3=subject%3A&q-type0=all&q-field2=creator%3A&q-type1=all&l-availability=y%2Ff&q=+date%3A[2000+TO+2014]&q-year2-date=2014&n=1', function(error, response, html) { if (!error && response.statusCode == 200) { var $= cheerio.load(html, { xmlMode: true }); console.log($('work').attr('id')) } });