javascript - 如何从网页中提取文本

标签 javascript android

首先,是的,我已经对这个问题进行了研究。是的,我找到了答案 here .但是整个过程仍然不适合我。我所需要做的就是从 Google 之类的网页上抓取文本,然后从抓取的文本中创建一个字符串。这是我的代码,其中包含上述教程代码:

public class Searching_Animation_Screen extends ActionBarActivity {
TextView loading_txt;
Animation blink;
public String pre_split;
public String[] split_string;
TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchinganimationscreen);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    int width = getWindowManager().getDefaultDisplay().getWidth();
    loading_txt = (TextView)findViewById(R.id.loading);
    text =(TextView)findViewById(R.id.textView);
    Typeface pacifico_typeface = Typeface.createFromAsset(getAssets(), "fonts/pacifico.ttf");
   loading_txt.setTypeface(pacifico_typeface);
   loading_txt.setTextSize(width / 20);
   blink = AnimationUtils.loadAnimation(getApplicationContext(),
           R.anim.blink);
   loading_txt.setAnimation(blink);
   Begin();

}

private void Begin() {
    Intent SEARCH_INTENT = getIntent();
    pre_split=SEARCH_INTENT.getStringExtra("Search_Text");
    split_string = pre_split.split(" ");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_searchinganimationscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    String google_url ="https://www.google.com/#safe=active&q=";

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        text.setText(Html.fromHtml(result));
        //throw into summarizer
    }


    public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] {"www.google.com"});

    }
}

}

Android studio 表示从未使用过 readWebpage 以及实际的 DownloadWebPageTask 类。有任何想法吗?我希望此类在创建时立即运行。谢谢!

最佳答案

@Ethan,当然,我希望这是你想要的,只是在 onCreate 方法中添加了 readWebpage 方法,但我修改了它并删除了 View 对象,因为它没有被使用,

    public class Searching_Animation_Screen extends ActionBarActivity {
TextView loading_txt;
Animation blink;
public String pre_split;
public String[] split_string;
TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchinganimationscreen);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    int width = getWindowManager().getDefaultDisplay().getWidth();
    loading_txt = (TextView)findViewById(R.id.loading);
    text =(TextView)findViewById(R.id.textView);
    Typeface pacifico_typeface = Typeface.createFromAsset(getAssets(), "fonts/pacifico.ttf");
   loading_txt.setTypeface(pacifico_typeface);
   loading_txt.setTextSize(width / 20);
   blink = AnimationUtils.loadAnimation(getApplicationContext(),
           R.anim.blink);
   loading_txt.setAnimation(blink);
   Begin();

  //* call webpage here, 
  //* note, i removed passing the view object since it is not being used
  readWebpage()

}

 //* (modify) by remvoving it from the code below 
 //* and removing the view object since it is not being used
 public void readWebpage() {
     DownloadWebPageTask task = new DownloadWebPageTask();
     task.execute(new String[] {"http://www.google.com"});

 }

private void Begin() {
    Intent SEARCH_INTENT = getIntent();
    pre_split=SEARCH_INTENT.getStringExtra("Search_Text");
    split_string = pre_split.split(" ");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_searchinganimationscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    String google_url ="https://www.google.com/#safe=active&q=";

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        text.setText(Html.fromHtml(result));
        //throw into summarizer
    }

  }

}

关于javascript - 如何从网页中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532520/

相关文章:

android - AdMob 错误(扩展类 com.google.ads.AdView 时出错)

javascript - 这个 ES6 箭头函数代码块的作用是什么?

javascript - 使用 keyup 事件将更改动态应用到多个元素

javascript - 从 Javascript 打开书签对话框

android - "flutter run"卡在 "Running Gradle task ' assembleDebug"

javascript - Android webview 无法正确加载网站

安卓 SQLite : Failing to update records

javascript - 数据库插入后,Angularjs 通过 $http.get 服务刷新 View

javascript - 根据设备显示/加载不同的背景图像

android - HERE Maps Android 集成,MISSING_LIBRARIES 错误