0

我正在编写一个开源包,它读取带有城市自行车可用性状态的 HTML,并将数据转换为通过 API 提供的 JSON。

HTML

    <div id="gmap"><div class='marker' data-lat='32.096100' data-lng='34.821400' data-name='דרך בן גוריון 2 פינת אבא הלל - רמת-גן' data-id='652'>
        <div class='marker-popover' data-lat='32.096100' data-lng='34.821400'>
            <div class='title'>דרך בן גוריון 2 פינת אבא הלל - רמת-גן (652)</div>
            <div class='station-data'>
                <div class='address'>דרך בן גוריון 2 פינת אבא הלל - רמת-גן <a href='#' class='navi'>נווט לתחנה <i class='fa fa-location-arrow'></i></a></div>
                <span class='bikes'><i class='fa fa-bicycle'></i> אופניים זמינים: 9</span>
                <span><i class='fa fa-park'></i> עמודים פנויים: 1</span>
            </div>
            <!--<div class='nearby-stations'>
                <div class='subtitle'>תחנות קרובות</div>
                <ul></ul>
            </div>-->
        </div>
    </div><div class='marker' data-lat='32.099200' data-lng='34.825500' data-name='איצטדיון רמת-גן מול שער 16' data-id='651'>
    ...

代码

const path = require('path');
const Xray = require('x-ray');
const read = require('fs').readFileSync;

const html = read(path.resolve(__dirname, 'index.html'));
const xray = Xray();

// How to fetch the 'data-lat' property?
xray(html, '.marker', [{ bikes: '.bikes', title: '.title', lat: 'data-lat' }])((err, value) => {
  console.log(value[0]);
});

结果

请注意,lat缺少-

{ bikes: ' אופניים זמינים: 9',
  title: 'דרך בן גוריון 2 פינת אבא הלל - רמת-גן (652)' }

我的问题

我已经设法使用and获取s的内容,但我没有设法获取属性,例如or 。div.title.addressdata-latdata-id

如何div使用 X 射线读取房产?

4

1 回答 1

0

@成功了

const path = require('path');
const Xray = require('x-ray');
const read = require('fs').readFileSync;

const html = read(path.resolve(__dirname, 'index.html'));
const xray = Xray();

xray(html, '.marker', [
  {
    bikes: '.bikes',
    title: '.title',
    lat: '@data-lat' }, // <div class='marker' data-lat='32.096100' data-lng='34.821400' ...
])((err, value) => {
  console.log(value[0]);
});
于 2017-07-19T07:53:45.320 回答