termux使用记录

First Post:

Last Update:

Word Count:
993

Read Time:
4 min

前言:一次日记

我与初次认识termux大概是在上初中那会,当时termux还处于不冷不热的状态,最后还是在国光大佬写的一篇关于termux文章下系统的学习了相关的知识,这次经历可谓是对我的后来乃至现在都受益匪浅(因为当时没有电脑,只有手机),这篇文章涉及了很多程序语言,例如java,python,go…..等等,还有linux系统的相关指令,是及其优秀的一篇文章,当然这还有许多。

国光大佬的原文地址

termux利用python爬取b站并入库

首先保证 mysql 与 python3 完整安装,在 termux中 mysql 安装可用 pkg install mariadb
命令安装。

termux中mariabd的基本配置可以参考官方文档

termux官方文档
mariadb官方文档

过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#启动mysql服务(得到一个进程号(pid号),nohup: ignoring input and appending output to `nohup.out'提示也是正常的)
nohup mysqld &

#进入mysql(whoami为本机的登陆名)
mysql -u ${whoami}

#mysql内修改root密码,我这里习惯设置用户名密码都为root
use mysql;
set password for 'root'@'localhost' = password('root');

# 刷新权限
flush privileges;

# 创建数据库
create database bilidb;
use bilidb;
CREATE TABLE `hot` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hotword` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hotword` (`hotword`)
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=latin1;

#退出mysql
quit;

#需要关闭mysql服务可以掉进程
kill -9 PID(这个pid号启动服务的时候回显示)

运行python程序

1
2
3
4
# python +文件名
例子:python hello-world.py

# 注意,部分手机受安卓11系统应用文件的限制,termux可能无法访问其他文件目录的文件,所以建议将需要的文件放在termux当下的目录位置

编写爬虫程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#python程序
#字典
import requests
import json
import pymysql.cursors


#加头标识
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
}

product=[]
#获取传回数据
resp=requests.get('https://s.search.bilibili.com/main/hotword?mid=&buvid=98E676EF-F586-403E-8440-52A6836FA68713451infoc&jsonp=jsonp&callback=jsonCallback_bili_58910703464582290',headers=headers)

rest=resp.text.replace('jsonCallback_bili_58910703464582290(','').replace(')','')
#转换json键值对

json_data=json.loads(rest)

comments=json_data["list"]
# #把想要的数据装入列表中
proDict=[]
for item in comments:
proDict.append(item['keyword']+' ')
product.extend(proDict)

# 把数组里所有数据都转入字符串中(这里是学习用的,可以去除)
p=''.join(proDict)

print(p)


# 数据入库
# 连接数据库
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='bilidb',
charset='utf8',
cursorclass=pymysql.cursors.Cursor,
)

cur=conn.cursor()
#这里用replace解决数据入库时重复数据的问题,注意hotword字段要设置唯一约束
for item in proDict:
cur.execute("replace into hot(hotword) values('%s')" %(item))
#事务提交(这里尝试性的用了一个异常处理,嘿嘿可以去掉)
try:
conn.commit()
except AttributeError:
print("错误")
cur.close()

注:最上面的 import <模块名称>,中引入的模块需要确保已经安装,否则会出现模块缺失报错

最后将上面的文件保存后直接运行

在 termux 中依次输入下面图中的命令

在termux中加入定时任务,每分钟爬一次入库

1
2
3
4
5
6
7
8
9
10
#安装crontab
pkg install crontab
#启动服务
crond
#编辑定时任务
crontab -e
#输入定时任务
* * * * * python bili.py >>123.log
星号分别表示 分 时 天 月份 周 可以指定时间运行,若为星号就代表每分或每周或每时...运行,我这里是每分钟运行一次,若要每天则要确定具体时间即 0 8 * * * 这就表示每天8点运行(后面具体复杂操作见https://www.runoob.com/linux/linux-comm-crontab.html)
然后后面就是指令了,***注意的是后面必须要指定一个log文件***,不然它不会运行.

运行示例