0

我可以从我的 react.js 项目中获得DuckDuckGo的搜索结果。https://api.duckduckgo.com/但由于它只给我们即时答复结果,我无法获得完整的搜索结果。

这是我得到搜索结果的代码:

import React, { Component } from 'react';
import { render } from 'react-dom';
import axios from 'axios';

interface AppProps { }
interface AppState {
  name: string;
}

class App extends Component<AppProps, AppState> {
  constructor(props) {
    super(props);
    this.state = {
      name: 'React',
      query: '',
      img: "",
    };
  }

  search = () => {
    const options = {
      method: 'GET',
      url: 'https://api.duckduckgo.com/',
      params: {
        q: this.state.query,
        no_redirect: '1',
        no_html: '1',
        skip_disambig: '1',
        format: 'json'
      },
    };

    axios.request(options).then((res) => {
      const b = res.data;         // Search result is not satisfied for me

      if(b.Abstract != "") {
        this.setState({
          img: b.Image
        })
      }
    }).catch(function (error) {
      console.error(error);
    });
  }

  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>
          Start editing to see some magic happen :)
        </p>
        <input type="text" onChange={(e) => this.setState({query: e.target.value})} />
        <button type="button" onClick={this.search.bind(this)} >Search </button>
        { this.state.img !== "" && (<img src={`https://duckduckgo.com${this.state.img}`} alt="" />) }
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

有没有办法获得类似于网页抓取搜索结果页面的完整结果?

4

0 回答 0