中国土地管理局数据抓取

数据抓取时遇到以下几个问题:

 1.需要验证cookie

2.访问量大的话,封ip

3.详情页有woff字体加密

抓取过程中直接使用谷歌浏览器抓取数据,详情页链接;https://www.landchina.com/DesktopModule/BizframeExtendMdl/workList/bulWorkView.aspx?wmguid=20aae8dc-4a0c-4af5-aedf-cc153eb6efdf&recorderguid=JYXT_ZJGG_18725&sitePath=

分析后发现,使用的是woff字体加密,

下载woff字体文件:https://www.landchina.com/styles/fonts/gRYimlGgKp1mW5ldNU5LIoLTJdKXEnKo.woff?fdipzone

打开后:如下图

观察发现:$51E7 中$符号后面的内容是文字unicode值相对应,具体映射关系自己整理,这样的话基本上就没有难点了,直接上代码:

class Crawler():
    def __init__(self):
        #初始化字典
        self.font_map = {}
        with open('font_table.txt','r',encoding='utf-8') as fp:
            for i in fp:
                infos = i.split(':')
                self.font_map[infos[0].strip()[2:]] = infos[1].strip()


    def cn_to_unicode(self,in_str, need_str=True, debug=False):
        out = []

        for s in in_str:
            # 获得该字符的数值
            val = ord(s)
            # print(val)

            # 小于0xff则为ASCII码,手动构造\u00xx格式
            if val <= 0xff:
                hex_str = hex(val).replace('0x', '').zfill(4)
                # 这里不能以unicode_escape编码,不然会自动增加一个'\\'
                res = bytes('\\u' + hex_str, encoding='utf-8')
            else:
                res = s.encode("unicode_escape")

            out.append(res)

        # 调试
        if debug:
            print(out)
            print(len(out), len(out[0]), len(out[-1]))

        # 转换为str类
        if need_str:
            out_str = ''
            for s in out:
                out_str += str(s, encoding='utf-8')
            return out_str
        else:
            return out

    def unicode_to_cn(self,in_str, debug=False):
        out = None

        if isinstance(in_str, bytes):
            temp = str(in_str, encoding='utf-8')
            out = temp.encode('utf-8').decode('unicode_escape')
        else:
            out = in_str.encode('utf-8').decode('unicode_escape')

        return out

    def html_trans(self,content):
        content_new = copy.deepcopy(content)
        for i in content:
            str_ = self.cn_to_unicode(i)
            new_str = self.font_map.get(str_[2:].upper(), '')
            if new_str:
                content_new = content_new.replace(i, new_str)
        return content_new
    
    def process(self):
        str_ = '温岭黨自然厛剨和凢划舉逡鋽鋼箹使賗饈趬輕出让瀍飜(温鋼瀍飜鑽[2021]整崬12号)'
        content = self.html_trans(str_)

最后的结果:温岭市自然资源和规划局国有建设用地使用权挂牌出让公告(温土公告字[2021]整第12号)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值