目标网址:猫眼电影
主要流程
- 爬取每一个电影所对应的url
- 爬取具体电影所对应的源码
- 解析源码,并下载所对应的字体
- 使用 fontTools 绘制所对应的数字
- 运用机器学习的方法识别对应的数字
- 在源码中用识别的数字替换相应的地方
遇坑经历
- 用 pyquery 的 .text() 方法的时候自动对 html 进行了反转义,替换过程失败,直接打出来一堆乱码,不得已改用 lxml
- 一开始在网上看到很多通过将 woff 字体文件转为 xml 并通过分析 xml 中的 glyf 来判断所对应的汉字,但是看了几个之后发现即使是相同的数字所对应的 x,y值也并不是完全相同
- 对多线程的用法还不是很熟悉
项目列表如下
具体代码如下
Myspider.py
import requests
import fake_useragent
import re
import os
from woffToPng import woff_to_image
from resizeImage import resize_img
from lxml import etree
from html import unescape
from ThreadClass import SpiderThread
from SaveMovieData import SaveInfo
from pyquery import PyQuery as pq
from pprint import pprint
import json
import time
# 用协程实现有点苦难,暂时试一试多线程
# 存储urls
film_urls = []
def verify_img(img_dir):
api_url = "http://127.0.0.1:6000/b"
img_to_num = {
}
for file in os.listdir(img_dir):
file_name = os.path.join(img_dir, file)
# 重新构建图片大小
resize_img(file_name, file_name)
files = {
"image_file": ("image_file", open(file_name, "rb"), "application")}
r = requests.post(url=api_url, files=files, timeout=None)
if r.status_code == 200:
# 获得图片的名字,即数字所对应的unicode编码
num_id = os.path.splitext(file)[0][3:]
img_to_num[str(int(num_id, 16))] = r.json().get("value")
return img_to_num
def find_certain_part(html, xpath_format):
try:
return html.xpath(xpath_format)[0]
except Exception:
return "null"
def parse_data_by_lxml(source_code, img_to_num, saver):
html = etree.HTML(source_code)
xpaths = json.loads(open("somexpaths.json", "r").read())
movie_name = find_certain_part(html, xpaths.get("movie_name"))
movie_ename = find_certain_part(html, xpaths.get("movie_ename"))
movie_classes = find_certain_part(html, xpaths.get("movie_classes")).strip()
movie_length = find_certain_part(html, xpaths.get("movie_length")).strip()
movie_showtime = find_certain_part(html, xpaths.get("movie_showtime")).strip()
text_pattern = re.compile('.*?class="stonefont">(.*?)</span>')
data_to_be_replace = []
movie_score = find_certain_part(html, xpaths.get("movie_score"))
movie_score_num = find_certain_part(html, xpaths.get("movie_score_num"))
if movie_score != "null":
movie_score = text_pattern.search(etree.tostring(movie_score).decode("utf8")).group(1)
if movie_score_num != "null":
movie_score_num