# selenium_use
**Repository Path**: HITWH-RJGC/selenium_use
## Basic Information
- **Project Name**: selenium_use
- **Description**: 一个基于selenium爬取数据的小东西
- **Primary Language**: Python
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2020-04-12
- **Last Updated**: 2024-02-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# selenium 爬虫开发
## 开发环境
1. python3.8 + pip3
- sudo apt install aria2
- aria2c
- tar -xvJf Python-3.8.2.tar.xz
- cd Python-3.8.2
- ./configure&&make&&sudo make install
- sudo apt install pip3
2. selenium
- sudo pip3 install selenium
3. geckodriver + Firefox
- aria2c
- tar -xvzf geckodriver-v0.26.0-linux64.tar.gz
- sudo mv geckodriver /usr/local/bin
4. sqlalchemy + PyMySql + Mysql
## 开发进程
### 创建目录结构
1. database 数据库模块
2. crawler 爬虫模块
3. models 存放对每个网站爬虫脚本
4. test 测试脚本目录
5. config 配置文件目录
6. run.py 主启动脚本
### 项目配置
1. config/config.ini 默认配置
2. 添加各个配置项
3. 在run.py 中读入配置
### 爬虫模块
1. 模块执行过程
1. 引入startup 模块启动geckodriver
2. 引入database 模块启动数据库会话
3. 读取models 目录下的所有脚本 循环引入
4. 执行每个脚本中的执行函数run
5. 关闭geckodriver 和 数据库会话
2. selenium
1. xpath 使用
3. 数据库相关
1. 自动更新时间
2. 时间排序
3. 时间差计算
### 数据库模块
1. 从配置获取数据库链接地址和参数
2. 启动数据库引擎和会话
## 数据库管理
1. 启动MySql 服务
- net start mysql
2. 登陆数据库管理系统
- mysql -u root -p
3. 创建数据库
- create database NewsCard;
4. 查询表结构
- desc tablname;
5. 更新操作
- UPDATE tablename SET field1=new-value1, field2=new-value2;
## 错误总结
1. Selenium 调用PhantomJS 被警告PhantomJS 已被弃用,改用Firefox
2. import 问题
3. crawler/\_init\_\_ 中使用 os.path.isfile(c) 识别c 是否为文件
但c 不在当前目录下所以补全路径后改正
4. 启动数据库服务时报错,需要管理员权限
5. 爬取网页上的新闻时间格式不一,有些python 不能直接识别为时间格式
通过转换才行
# Footnotes
1 [安装selenium以及chromedriver、geckodriver](https://blog.csdn.net/qq_23926575/article/details/77268924)
2 [读取ini配置文件](https://www.jianshu.com/p/474ef3a8cfd3) [读写配置](https://blog.csdn.net/songlh1234/article/details/83316468)
3 [python中selenium使用](https://blog.csdn.net/One_of_them/article/details/82560880) [教程](https://www.yiibai.com/selenium/seleniumwebdriver.html) [系列](https://www.cnblogs.com/themost/p/6900852.html) [用法](https://blog.csdn.net/qq_29186489/article/details/78661008?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10) [详解](https://www.cnblogs.com/themost/p/6900852.html) [官方](http://www.selenium.org.cn/category/python) [和firefox](https://www.cnblogs.com/pythonClub/p/9736361.html)
4 [Xpath W3C](https://www.w3school.com.cn/xpath/xpath_syntax.asp)
5
6 [时间降序排序](http://www.itspire.cn/article/61.html)
7 [时间差计算](https://www.liaoxuefeng.com/wiki/1016959663602400/1017648783851616)
8 [sqlalchemy 技术文档](https://www.jianshu.com/p/0ad18fdd7eed) [官方](https://www.osgeo.cn/sqlalchemy/intro.html)
9 [MySql 数据库](https://www.runoob.com/mysql/mysql-administration.html)
10 [\_init\_\_](https://www.cnblogs.com/Lands-ljk/p/5880483.html) [字符串导入](https://blog.csdn.net/chaoguo1234/article/details/81277590) [import 详解](https://blog.csdn.net/weixin_38256474/article/details/81228492)
11 [python datetime](https://blog.csdn.net/weixin_37989267/article/details/89100444?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3)