Javascript 如何从网页中提取文本
首先,是的,我对这个问题做了研究。是的,我找到了答案。但整个过程对我来说仍然不起作用。我所需要做的就是从谷歌这样的网页上抓取文本,然后从它抓取的文本中创建一个字符串。以下是我的代码,其中包含上述教程代码:Javascript 如何从网页中提取文本,javascript,android,Javascript,Android,首先,是的,我对这个问题做了研究。是的,我找到了答案。但整个过程对我来说仍然不起作用。我所需要做的就是从谷歌这样的网页上抓取文本,然后从它抓取的文本中创建一个字符串。以下是我的代码,其中包含上述教程代码: public class Searching_Animation_Screen extends ActionBarActivity { TextView loading_txt; Animation blink; public String pre_split; public String[]
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"});
}
}
}
公共类搜索\u动画\u屏幕扩展了ActionBarActivity{
文本视图加载_txt;
动画闪烁;
公共字符串预分割;
公共字符串[]拆分字符串;
文本查看文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u searchinganimationscreen);
ActionBar ActionBar=getSupportActionBar();
actionBar.hide();
int width=getWindowManager().getDefaultDisplay().getWidth();
加载_txt=(TextView)findViewById(R.id.loading);
text=(TextView)findViewById(R.id.TextView);
Typeface pacifico_Typeface=Typeface.createFromAsset(getAssets(),“font/pacifico.ttf”);
加载txt.setTypeface(pacifico_字体);
加载_txt.setTextSize(宽度/20);
blink=AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);
加载_txt.setAnimation(闪烁);
Begin();
}
私有void Begin(){
意图搜索_Intent=getIntent();
pre_split=SEARCH_INTENT.getStringExtra(“SEARCH_Text”);
拆分字符串=预拆分。拆分(“”);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u搜索动画屏幕,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私有类下载WebPagetTask扩展异步任务{
字符串google_url=”https://www.google.com/#safe=active&q=";
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串响应=”;
for(字符串url:url){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
试一试{
HttpResponse execute=client.execute(httpGet);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(
新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null){
响应+=s;
}
}捕获(例外e){
e、 printStackTrace();
}
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
text.setText(Html.fromHtml(结果));
//扔进摘要器
}
公共作废阅读网页(查看){
DownloadWebPagetTask=新建DownloadWebPagetTask();
execute(新字符串[]{“www.google.com”});
}
}
}
Android studio表示,除了实际的
下载WebPageTask
类之外,从未使用过readWebpage
。有什么想法吗?我希望这个类在创建时立即运行。谢谢 @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
}
}
}
公共类搜索\u动画\u屏幕扩展了ActionBarActivity{
文本视图加载_txt;
动画闪烁;
公共字符串预分割;
公共字符串[]拆分字符串;
文本查看文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u searchinganimationscreen);
ActionBar ActionBar=getSupportActionBar();
actionBar.hide();
int width=getWindowManager().getDefaultDisplay().getWidth();
加载_txt=(TextView)findViewById(R.id.loading);
text=(TextView)findViewById(R.id.TextView);
Typeface pacifico_Typeface=Typeface.createFromAsset(getAssets(),“font/pacifico.ttf”);
加载txt.setTypeface(pacifico_字体);
加载_txt.setTextSize(宽度/20);
blink=AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);
加载_txt.setAnimation(闪烁);
Begin();
//*点击这里的网页,
//*注意,我删除了传递视图对象,因为它没有被使用
阅读网页()
}
//*(修改)从下面的代码中删除
//*以及删除视图对象,因为它未被使用
公开阅读网页(){
DownloadWebPagetTask=新建DownloadWebPagetTask();
task.execute(新字符串[]{”http://www.google.com"});
}
私有void Begin(){
意图搜索_Intent=getIntent();
pre_split=SEARCH_INTENT.getStringExtra(“SEARCH_Text”);
拆分字符串=预拆分。拆分(“”);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u搜索动画屏幕,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私有类下载WebPagetTask扩展异步任务{
字符串google_url=”https://www.google.com/#safe=active&q=";
@凌驾
保护