【问题标题】:How Do We Parse Info From These Web Pages?我们如何从这些网页中解析信息?
【发布时间】:2016-09-30 21:33:06
【问题描述】:

我和我的朋友在我们正在尝试为学校项目创建的一个小应用程序方面需要帮助。首先,此应用程序的目的是在我们居住的地方寻找药房。这两个是我试图从中提取信息的网站(这是否称为拉取?)

http://www.istanbulsaglik.gov.tr/w/nobet/liste.asp?lc=0&gun=09.05.2015

  • gun = 日期/日期

  • lc = 城镇 ID

  • cs 每天给一个城镇,每天都在变化,它只是一个数字。

在这个页面中。

与/或

http://www.nobetcieczanebul.com/

这个“看起来”更容易获得所需的信息。来自。

它们都包含我需要的信息;药房的名称,地址,可能还有电话号码。通过使用 JSOUP,我希望解析(提取?)这些信息,但我需要帮助。我的首要任务是使用“webview”。

首先我们从 HTML 中读取 cs,然后我们将 cs 放入代码中并调用所有药房的名称、日期和地址。

这是我的示例代码,此代码调用 id 32 town of date 05/05/2015 其中包含名称等。

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class MainActivity extends ActionBarActivity {

Button btnGetir;
ProgressBar pb;
TextView tv;
Button btnNobet;
TextView tvNobet;
WebView wv;

class mytask extends AsyncTask<Void,Void,Void>{
String Title;
@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    pb.setVisibility(View.INVISIBLE);
tv.setText(Title);

}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pb.setVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... params) {

    try {
        Document myDoc= Jsoup.connect("http://www.ticaret.edu.tr").get();
        Title=myDoc.title();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
    }
   }

   class myNobettask extends AsyncTask<Void,Void,Void>{
    String Title;
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        pb.setVisibility(View.INVISIBLE);
        wv.loadData(Title, "text/html", "UTF-8");

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Document myDoc= Jsoup.connect("http://apps.istanbulsaglik.gov.tr/eczane/GetNobetciEczaneler.aspx?lc=32&gun=05.05.2015&cs=6e79f453").get();


            Title=myDoc.html();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
btnGetir=(Button) findViewById(R.id.btnTitleGetir);
tv=(TextView)findViewById(R.id.tvSonuc);
    pb=(ProgressBar) findViewById(R.id.progressBar);

    btnNobet =(Button)findViewById(R.id.btnNobetci);
    tvNobet=(TextView)findViewById(R.id.tvNobetci);
wv=(WebView) findViewById(R.id.webView);

    btnGetir.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mytask newtask= new mytask();
            newtask.execute();
        }
    });


    btnNobet.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myNobettask task= new myNobettask();
            task.execute();
        }
    });
 }


@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_main, 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);
    }
}

【问题讨论】:

  • 是否必须使用这些网站之一? ,因为可以使用Google Places API,效率更高并且提供了json o xml响应(易于解析)
  • 我“相信”是因为我们正在寻找值班药房。不知道这样说对不对,值班药房--> 日夜营业的药店,急救药房。这两个网站就是展示它的网站。 Google Places API 会显示它们吗?
  • 以下是 Google API 可以提供的所有类型的列表:developers.google.com/places/supported_types
  • 嗯,你说得对,我们可以使用它,你有任何示例代码可以嵌入到我们的代码中吗?
  • 我会把它作为答案发布

标签: android html webview jsoup


【解决方案1】:

使用 JSoup,您应该能够指定要解析的 CSS 选择器/ HTML 组件。

Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); // This should return a collection of links.

可以进一步指定,所以也许您希望返回所有段落标签的子集,然后您可以指定:

Document doc = Jsoup.connect(url).get(); Elements linksInPTags= doc.select("p > a[href]");

通读Jsoup cookbook。它非常清晰,非常有帮助。

【讨论】:

    【解决方案2】:

    这是一个使用Google Places API 的sn-p 代码,可能非常有帮助:

        public static void makeRequest(String url)
        {
    
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url); 
            HttpResponse response;
            try {
                response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    String result= convertStreamToString(instream);
                    instream.close();
                }
            } catch (Exception e) {}
        }
    
        private String convertStreamToString(InputStream is) {
            BufferedReader.readLine()
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    

    完成步骤:

    1 - 您必须生成满足您需求的 URL(例如:https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=AddYourOwnKeyHere) (创建 URL 的完整描述在上面的链接中)

    2 - 在后台线程中使用此方法是可选的,但更可靠

    3 - 您必须解析响应(响应可以是 json 或 xml 格式,您可以从 URL 中选择)数据来执行您的需求(您可以从上面的链接中获取响应字段描述)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2011-10-29
      • 2012-04-20
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多