数据库的表跟APP的运行截图;

 

 

 

 

 

首先需要将爬取的数据存进MySQL数据库;需要用到python的爬取;下面是代码,跟上一次的大作业一样,爬取技术;

 1 import pymysql
 2 import requests
 3 import json
 4 # 放入要爬的url
 5 url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
 6 # 设置header做一个防爬机制
 7 header = {"user-agent": "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Mobile Safari/537.36"}
 8 # 获取response的json
 9 response = requests.get(url, headers=header)
10 # 取得数据词典
11 data = json.loads(response.content.decode())
12 data_str = data[\'data\']
13 data_json = json.loads(data_str)
14 # 连接数据库
15 db = pymysql.connect(host = \'localhost\', port=3306, user=\'root\', password=\'123456\', db=\'python\', charset=\'utf8\')
16 #使用cursor方法生成一个游标
17 cursor = db.cursor()
18 confirmed_total = 0
19 suspected_total = 0
20 dead_total = 0
21 healed_total = 0
22 # 更新时间
23 lastUpdateTime = data_json[\'lastUpdateTime\']
24 # 取出各个国家的数据
25 for worldData in data_json[\'areaTree\']:
26     countryName = worldData[\'name\']
27     confirmed = worldData[\'total\'][\'confirm\']
28     confirmed_total += confirmed
29     suspected = worldData[\'total\'][\'suspect\']
30     suspected_total += suspected
31     dead = worldData[\'total\'][\'dead\']
32     dead_total += dead
33     healed = worldData[\'total\'][\'heal\']
34     healed_total += healed
35     sql = "insert into worlddata(id,countryname,confirmed,suspected,dead,healed,lastupdateTime) values({},\'{}\',\'{}\',\'{}\',\'{}\', \'{}\',\'{}\')".format(0, countryName, confirmed, suspected, dead, healed, lastUpdateTime)
36     cursor.execute(sql)
37 
38     db.commit()
39 sql_total = "insert into worlddata(id,countryname,confirmed,suspected,dead,healed,lastupdateTime) values({},\'{}\',\'{}\',\'{}\',\'{}\', \'{}\',\'{}\')".format(0, 0, confirmed_total, suspected_total, dead_total, healed_total, lastUpdateTime)
40 cursor.execute(sql_total)
41 db.commit()

爬取

然后你需要用到Androidstudio连接mysql数据库

 连接前需要将你eclipse里面的mysql-connector-java这个复制粘贴到lib文件夹下

 

然后再file->添加进去;

 

 

 

 

然后就是先测试你是否可以连接上数据库;这个你在网上搜一下,就可以找到代码;

 

 

package com.example.anew;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends Activity {

    private EditText et_name;
    private Button btn_get_data;
    private TextView tv_data;
    private RadioGroup rg_check;
    private RadioButton rb_date;
    private RadioButton rb_country;
    private String condition;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 0x11:
                    String s = (String) msg.obj;
                    tv_data.setText(s);
                    break;
                case 0x12:
                    String ss = (String) msg.obj;
                    tv_data.setText(ss);
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 控件的初始化
        btn_get_data = findViewById(R.id.btn_get_data);
        tv_data = findViewById(R.id.tv_data);
        et_name = findViewById(R.id.et_name);
        rb_date = findViewById(R.id.rb_date);
        rb_country = findViewById(R.id.rb_country);
        rg_check = findViewById(R.id.rg_select);


        rg_check.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //如果‘时间’这个单选按钮被选中了
                if(rb_date.getId()==checkedId){
                    //弹出吐司通知
                    //Toast.makeText(MainActivity.this, rb_date.getText().toString(), Toast.LENGTH_LONG).show();
                    //获取选中按钮对应的文本信息
                    condition = rb_date.getText().toString().trim();
                }else if(rb_country.getId()==checkedId){
                    //Toast.makeText(MainActivity.this, rb_country.getText().toString(), Toast.LENGTH_LONG).show();
                    condition = rb_country.getText().toString().trim();
                }
            }
        });
        //如果没有选择默认按时间查询
        if (condition == null){
            condition = rb_date.getText().toString().trim();
        }
        setListener();
    }

    /**
     * 设置监听
     */
    private void setListener() {

        // 按钮点击事件
        btn_get_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 创建一个线程来连接数据库并获取数据库中对应表的数据
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String name = et_name.getText().toString().trim();
                        //调用数据库帮助类中的方法取数据
                        List<information> list = DBUtils.search(condition,name);
                        Message message = handler.obtainMessage();
                        if (list != null) {
                            String s = "";
                            for (int i = 0; i < list.size(); i++) {
                                s += "国家:" + list.get(i).getCountryname() + "\n";
                                s += "最新更新时间:" + list.get(i).getLastUpdateTime() + "\n";
                                s += "确诊人数为:  " + list.get(i).getConfirmed() + "\n";
                                s += "治愈人数为:  " + list.get(i).getHealed() + "\n";
                                s += "死亡人数为:  " + list.get(i).getDead() + "\n" + "\n";
                            }
                            //0x11、0x12消息的定位标志
                            message.what = 0x12;
                            message.obj = s;
                        } else {
                            message.what = 0x11;
                            message.obj = "查询结果为空";
                        }
                        handler.sendMessage(message);

                        // 发消息通知主线程更新UI
                    }
                }).start();
            }
        });

    }

}

 

 1 package com.example.anew;
 2 
 3 
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 /**
13  * 数据库工具类:连接数据库用、获取数据库数据用
14  * 相关操作数据库的方法均可写在该类
15  */
16 public class DBUtils {
17 
18     private static String driver = "com.mysql.jdbc.Driver";// MySql驱动
19 
20     private static String user = "root";// 用户名
21 
22     private static String password = "123456";// 密码
23 
24     private static Connection getConn(String dbName) {
25 
26         Connection connection = null;
27         try {
28             Class.forName(driver);// 动态加载类
29             String ip = "192.168.43.209";// 写成本机地址,不能写成localhost,同时手机和电脑连接的网络必须是同一个
30 
31             // 尝试建立到给定数据库URL的连接
32             connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/" + dbName + "?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" ,
33                     user, password);
34 
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38 
39         return connection;
40     }
41 
42     public static List<information>  search(String condition, String country_name){
43         List<information> list = new ArrayList<>();
44         Connection connection = getConn("python");
45         String sql = "";
46         //System.out.println(condition);
47         //选择条件
48         if(condition.equals("国家")){
49             //模糊查询
50             sql = "select * from worlddata where countryname like ?";
51         }
52         if(condition.equals("时间")){
53             sql = "select * from worlddata where lastUpdateTime like ?";
54         }
55 
56         System.out.println(country_name);
57         if(connection !=null){
58             try {
59                 PreparedStatement ps = connection.prepareStatement(sql);
60                 if(ps!=null){
61                     ps.setString(1,"%"+country_name+"%");
62                     ResultSet rs = ps.executeQuery();
63                     if(rs!=null){
64                         while(rs.next()){
65                             information worldData = new information();
66                             worldData.setId(rs.getInt("id"));
67                             worldData.setCountryname(rs.getString("countryname"));
68                             worldData.setConfirmed(rs.getString("confirmed"));
69                             worldData.setSuspected(rs.getString("suspected"));
70                             worldData.setDead(rs.getString("dead"));
71                             worldData.setHealed(rs.getString("healed"));
72                             worldData.setLastUpdateTime(rs.getString("lastUpdateTime"));
73                             list.add(worldData);
74                         }
75                         connection.close();
76                         ps.close();
77                         return list;
78                     }else{
79                         return null;
80                     }
81                 }else{
82                     return null;
83                 }
84             } catch (SQLException e) {
85                 e.printStackTrace();
86                 return null;
87             }
88         }else{
89             return null;
90         }
91 
92 
93     }
94 
95 }

DBUtils

package com.example.anew;


public class information {
    private int id;
    private String countryname;
    private String confirmed;
    private String suspected;
    private String dead;
    private String healed;
    private String lastUpdateTime;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCountryname() {
        return countryname;
    }

    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }

    public String getConfirmed() {
        return confirmed;
    }

    public void setConfirmed(String confirmed) {
        this.confirmed = confirmed;
    }

    public String getSuspected() {
        return suspected;
    }

    public void setSuspected(String suspected) {
        this.suspected = suspected;
    }

    public String getDead() {
        return dead;
    }

    public void setDead(String dead) {
        this.dead = dead;
    }

    public String getHealed() {
        return healed;
    }

    public void setHealed(String healed) {
        this.healed = healed;
    }

    public String getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(String lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
}

information

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="15dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RadioGroup
            android:id="@+id/rg_select"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="20dp"
            android:layout_marginTop="20dp">

            <RadioButton
                android:id="@+id/rb_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="时间"
                android:textSize="20sp"
                android:checked="true"/>

            <RadioButton
                android:id="@+id/rb_country"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="国家"
                android:textSize="20sp"/>

        </RadioGroup>
        <EditText
            android:id="@+id/et_name"
            android:padding="10dp"
            android:textSize="16sp"
            android:gravity="center"
            android:hint="输入要查询的内容"
            android:layout_below="@id/rg_select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn_get_data"
            android:layout_margin="15dp"
            android:textSize="16sp"
            android:text="查询"
            android:layout_below="@id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_data"
            android:padding="10dp"
            android:textSize="16sp"
            android:gravity="center"
            android:text="内容"
            android:layout_below="@+id/btn_get_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

</ScrollView>

mainactivity.xml

 

版权声明:本文为1234yyf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/1234yyf/p/12547094.html