如何从网页中提取文本 [英] How to extract text from a WebPage

查看:102
本文介绍了如何从网页中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,是的,我对这个问题做过研究。是的,我已经在这里找到了答案。但整个过程仍然不是为我工作。所有我需要做的就是抢文本关闭一个像谷歌网页,并从它创建抓住文本字符串。这里是我的code在它前面提到的教程code:

First off, yes, I have done research on this question. And yes, I have found an answer here. But the whole process still isn't working for me. All I need to do is grab text off of a webpage like Google, and create a string from the text it grabs. Here is my code with the aforementioned tutorials code in it:

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的工作室是在说 readWebpage 从未使用过,与实际 DownloadWebPageTask 类一起。有任何想法吗?我想这个类立即上运行的创建。谢谢!

Android studio is saying that readWebpage is never used, along with the actual DownloadWebPageTask class. Any ideas? I would like this class to run immediately on Create. Thanks!

推荐答案

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

@Ethan, sure, I hope this is what you want, just adding the readWebpage method in the onCreate method, but I modified it and removed the View object since it is not being used,

    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
    }

  }

}

这篇关于如何从网页中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆