数据抓取时遇到以下几个问题:
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号)