将抓取结果转换为数组

时间:2019-07-01 14:10:45

标签: php html arrays web-scraping

我正在使用简单HTML DOM抓取网站,输出如下所示:

// Map a set of min/max values to another set of min/max values based on the given value
const map = (value, x1, y1, x2, y2) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;

class Rotation extends Component {
    startX = 0;
    lastX = 0;
    pointerDown = false;

    state = {
        rotation: 1,
    };

    componentDidMount() {
        window.addEventListener('pointerdown', this.handlePointerDown);
        window.addEventListener('pointerup', this.handlePointerUp);
        window.addEventListener('pointermove', this.handlePointerMove);
    }

    handlePointerDown = event => {
        this.startX = event.pageX;
        this.pointerDown = true;
    };

    handlePointerUp = () => {
        this.pointerDown = false;
    };

    handlePointerMove = event => {
        if (!this.pointerDown) {
            return;
        }
        const rotation = Math.round(map(event.pageX, 0, window.innerWidth, 1, 360));
        this.setState({rotation});
    };

    render() {
        return <img src={`/img/rotation/${this.state.rotation}.png`}/>
    }
}

这是我的代码:

<tr>
    <th>Satuan</th>
    <th>Harga Barang 1</th>
    <th>Harga Barang 2</th>
    <th>Harga Barang 3</th>
    <th>Harga Barang 4</th>
</tr>
<tr>
    <td>0.5</td>
    <td>Rp 388.000</td>
    <td>Rp 342.000</td>
    <td>Rp 456.000</td>
    <td>Rp 377.000</td>
</tr>
<tr>
    <td>1.0</td>
    <td>Rp 725.000</td>
    <td>Rp 676.000</td>
    <td>Rp 855.000</td>
    <td>Rp 684.000</td>
</tr>

如何将输出转换为数组?

1 个答案:

答案 0 :(得分:1)

这是摘要,

MutableList

输出

$ret     = $html->find('tr');
$i       = true;
$headers = [];
foreach ($ret as $key => $value) {
    if ($i) {
        // fetching headers of first row
        foreach ($value->find('th') as $cell) {
            $headers[] = $cell->plaintext;
        }
    } else {
        $temp = [];
        // fetching pending values of td
        foreach ($value->find('td') as $cell) {
            $temp[] = $cell->plaintext;
        }
        // combining headers with values fetched from not first row
        $result[] = array_combine($headers, $temp);
    }
    $i = false;
}
print_r($result);die;