记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

 猫眼票房排行榜页面显示如下:

注意右边的票房数据显示,爬下来的数据是这样显示的:

网页源代码中是这样显示的:

 

 

这是因为网页中使用了某种字体的缘故,分析源代码可知:

 

亲测可行:

  代码中获取的是国内票房榜,稍加修改也可适用于最受期待榜和北美票房榜

 

解决思路如下:

1.获取网页数据后,查找字体信息,获取到字体链接,下载字体保存到本地

2.使用fontTools读取字体中的字符集,并构造字典(依据基准字体)

3.根据字典,替换网页中的相关数据信息。

 

 注意:如果使用BeautifulSoup一定要先使用字典替换字符集,再解析。直接解析BeautifulSoup会将无法识别的字符置为空。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" 
@Project:pachong
@author:sandu
@Email: sandu12345@msn.cn
@Software: PyCharm
@file: test_maoyan.py 
@time: 2019-01-08 0008 上午 10:05 
"""

import csv
import json
import os
import re
from hashlib import md5

import pymongo
import requests
from fontTools.ttLib import TTFont
from requests.exceptions import RequestException

import woff2otf

MONGO_URL = 'localhost'
MONGO_DB = 'maoyan'
MONGO_TABLE = 'maoyan_beimei'

client = pymongo.MongoClient(MONGO_URL, connect=False)
db = client[MONGO_DB]


# 获取单页数据
def get_one_page(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None


# 解析单页数据,获取所需的数据
# '.*?board-index.*?>(d+).*?' 获取顺序号
# '.*?data-src="(.*?)".*?'    获取图片链接
# '.*?name.*?title.*?>(.*?)</a>.*?'  获取电影名称
# '.*?star">(.*?)</p>.*?',re.S 获取演员名单(有换行,需要加上re.S,否则获取不到数据)
# '.*?releasetime">(.*?)</p>.*?' 获取上映时间
# '.*?integer">(.*?)</i>.*?' 获取主分
# '.*?fraction">(.*?)</i>.*?'获取辅分

# '.*?realtime.*?stonefont">(.*?)</span></span>(.*?)</p>.*?' 实时票房
# '.*?total-boxoffice.*?stonefont">(.*?)</span></span>(.*?)</p>.*?' 总票房
# 综合下来,加上最外层的dd

def parse_one_page(html):
    pattern = re.compile(
        '<dd>.*?board-index.*?>(d+).*?data-src="(.*?)".*?name.*?title.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?realtime.*?stonefont">(.*?)</span></span>(.*?)</p>.*?total-boxoffice.*?stonefont">(.*?)</span></span>(.*?)</p>.*?</dd>',
        re.S)
    items = re.findall(pattern, html)
    for item in items:
        yield {
            'index': item[0],
            'img': item[1],
            'name': item[2].strip(),  # 去除前后空格换行符等
            'star': item[3].strip()[3:],  # 去除前后空格换行符等,切片截取指定的范围
            'time': item[4][5:],
            '实时票房': item[5] + item[6].strip(),
            '总票房': item[7] + item[8].strip(),
        }


#  保存至文件
def save_to_file(content):
    # 注意:把json数据保存到文件中显示出中文
    with open('beimei.text', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '
')


# 保存到数据库中
def save_to_mongo(result):
    if db[MONGO_TABLE].insert(result):
        print('Successfully Saved to Mongo', result)
        return True
    return False


# 请求图片url,获取图片二进制数据
def download_image(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            save_image(response.content)  # response.contenter二进制数据 response.text文本数据
        return None
    except RequestException:
        print('请求图片出错')
        return None


# 数据存储到csv
def write_to_file3(item):
    with open('beimei.csv', 'a', encoding='utf_8_sig', newline='') as f:
        # 'a'为追加模式(添加)
        # utf_8_sig格式导出csv不乱码
        fieldnames = ['index', 'img', 'name', 'star', 'time', '实时票房', '总票房']
        w = csv.DictWriter(f, fieldnames=fieldnames)
        w.writerow(item)


# 解析字体
def get_font_regx(html):
    p = re.compile(r"url('(.*?)')sformat('woff');")  # 查找网页上的字体链接
    uni_font_url = re.findall(p, html)
    url = 'http:%s' % uni_font_url[0]
    resp = requests.get(url)
    with open('maoyan.woff', 'wb') as fontfile:
        for chunk in resp.iter_content(chunk_size=1024):
            if chunk:
                fontfile.write(chunk)  # 将字体下载到本地
    woff2otf.convert('maoyan.woff', 'maoyan.otf')
    baseFont = TTFont('base.otf')  # base.otf是某一次访问获取的字体文件,然后人工识别内容,作为与后面获取字体的比对标本,从而让电脑自动获得后面获取字体的实际内容。
    maoyanFont = TTFont('maoyan.otf')
    uniList = maoyanFont['cmap'].tables[0].ttFont.getGlyphOrder()  # 解析otf字体后获得的数据
    numList = []  # 解析otf字体数据转换成数字
    baseNumList = ['.', '3', '5', '1', '2', '7', '0', '6', '9', '8', '4']
    baseUniCode = ['x', 'uniE78E', 'uniF176', 'uniEFE6', 'uniF074', 'uniE9C8', 'uniE912', 'uniEA71', 'uniE74E',
                   'uniE4B8', 'uniEE71']
    for i in range(1, 12):
        maoyanGlyph = maoyanFont['glyf'][uniList[i]]
        for j in range(11):
            baseGlyph = baseFont['glyf'][baseUniCode[j]]
            if maoyanGlyph == baseGlyph:
                numList.append(baseNumList[j])
                break
    uniList[1] = 'uni0078'
    new_dict = dict(zip(uniList[2:], numList[1:]))  # 实时获取字体映射关系
    html = html.replace('&#x', 'uni')
    for key in new_dict.keys():
        initstr = key.lower() + ';'
        html = html.replace(initstr, new_dict[key])
    return html


def save_image(content):
    file_path = '{0}/{1}.{2}'.format(os.getcwd(), md5(content).hexdigest(), 'jpg')
    if not os.path.exists(file_path):
        with open(file_path, 'wb') as f:
            f.write(content)


def main(offset):
    url = 'https://maoyan.com/board/2?offset=' + str(offset)  # 针对分页
    html = get_one_page(url)
    html = get_font_regx(html)
    for item in parse_one_page(html):
        print(item)
        save_to_file(item)  # 保存至文件
        write_to_file3(item)  # 写入到csv文件中,一定要写在保存到数据库的前面,因为先保存到数据库后保存的字典数据中会多一个_id值
        save_to_mongo(item)  # 保存到数据库
        # download_image(item['img']) # 下载图片保存到当前目录


if __name__ == '__main__':
    # for i in range(0, 100):
    #     main(str(i*10))
    # 开启多线程
    # pool = Pool()
    # pool.map(main,0)

    main(0)

# 注:如何获取代码中base.otf相关信息?
# 1.根据获取到的woff字体文件,使用百度字体编辑器,获取字体数字等相关信息,地址:http://fontstore.baidu.com/static/editor/index.html


# 2.将获取到的woff文件使用woff2otf.convert('maoyan.woff', 'base.otf')转化成base.otf文件保存到当前目录(./woff2otf.py font.woff font.otf),从而获得baseFont(代码中变量)
# 3.根据百度字体编辑器获取到的信息,构造baseNumList和baseUniCode(代码中变量)

# 4.再次发起请求根据获得的字体跟这个构造的基准字体进行对照,从而获得新的字体映射关系

# 注: woff2otf是导入的一个py文件,链接地址:https://github.com/hanikesn/woff2otf,作用是输入woff字体,输出otf字体

# 保存到csv文件中

 


 

# 保存到MongoDB数据库中

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/10239761.html