XuSenfeng

个人站

复读了,更新随缘,有的文件不全或者图片缺失具体看我的笔记库(https://github.com/XuSenfeng/note)


环境搭建

目录

环境搭建

python3

(base) jiao@ubuntu:~/Downloads$ sudo apt-get install -y python3-dev build-essential libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev libcurl4-openssl-dev
(base) jiao@ubuntu:~/Downloads$ sudo apt-get install -y python3
(base) jiao@ubuntu:~/Downloads$ sudo apt-get install -y python3-pip


下载Anaconde

里面有多个常用的python库

[anaconda 镜像站使用帮助 清华大学开源软件镜像站 Tsinghua Open Source Mirror](https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/)

下载最新的

之后

(base) jiao@ubuntu:~/Downloads$ cd Downloads/
(base) jiao@ubuntu:~/Downloads$ chmod +x Anaconda3-2022.10-Linux-x86_64.sh 
(base) jiao@ubuntu:~/Downloads$ ./Anaconda3-2022.10-Linux-x86_64.sh 

下载相关的库

pip install requests
pip install selenium

后面的那个里面可以使用浏览器进行抓取, 比如点击下拉等

下载浏览器

  • 使用的是Chrome
  • 需要下载相应的驱动Chromedriver

这个相当于是个入口,启动还是本地事先安装好的Chrome浏览器,而且ChromeDriver只兼容相应的浏览器版本,因此,在下载前需要确定好自己得下哪个版本。

CNPM Binaries Mirror (npmmirror.com)

配置环境变量或者把可执行文件移动到/usr/bin
>>> from selenium import webdriver
>>> browser = webdriver.Chrome()

  • 下载地址:https://github.com/mozilla/geckodriver/releases 火绒相关的

  • phantomJS, 没有界面可以使用脚本编程的浏览器

下载的软件会在bin文件夹当中

在使用的时候不能使用高版本的

  pip uninstall selenium
  2.安装selenium==2.48.0版本的,代码如下:
  pip install selenium==2.48.0
  • aiohttp

requests是一个阻塞式的请求库, 发送一个请求之后会一直等待服务器的响应, 得到响应之后才会进行下一步的操作, 需要等待的时间比较长

aiohttp是一个异步的服务库

直接使用pip进行安装

相关的解析库

  • lxml相关的HTML和XML解析, 支持XPATH
  • Beautiful Soup同上, 依赖于上面的库
  • 安装pyquery
  • 安装tesserocr, 用于识别图片二维码, 实际上是对tesseract的封装(base) jiao@ubuntu:~$ sudo apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev
//查看可以支持的语言
(base) jiao@ubuntu:~$ tesseract --list-langs
List of available languages (2):
osd
eng

之后$ git clone https://github.com/tesseract-ocr/tessdata.git

下载文件全部复制到/usr/share/tesseract-ocr/4.0/tessdata文件夹中进行语言的添加

  • 使用
(base) jiao@ubuntu:~/Desktop$ tesseract R-C.png result -l eng && cat result.txt
Tesseract Open Source OCR Engine v4.0.0-beta.1 with Leptonica
Warning. Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 1385
C

TESSERACLT

第二个参数是文件名, 第三个参数是保存结果的文件名, -l指定要使用的语言库, 最后打开文件

>>> import tesserocr
>>> from PIL import Image
>>> image = Image.open('R-C.png')
>>> print(tesserocr.image_to_text(image))
C

TESSERACLT

也可以直接

>>> print(tesserocr.file_to_text('R-C.png'))
C

TESSERACLT

安装数据库

(base) jiao@ubuntu:~/Desktop$ sudo apt-get install -y mysql-server mysql-client

使用

(base) jiao@ubuntu:~/Desktop$ sudo service mysql start
(base) jiao@ubuntu:~/Desktop$ sudo service mysql stop 
(base) jiao@ubuntu:~/Desktop$ sudo service mysql restart