很有意思的一個字體反爬舒帮,58同城的一個網(wǎng)頁
(1)先看看網(wǎng)頁儿惫,很容易找到樣式中看到這樣式,當你選中就是正常的列荔,去掉就是亂碼
(2)這一看就是這個原因了敬尺,字體文件枚尼,可以去network下面搜索font,很容易下載
(3)關鍵是不僅僅只有一個字體庫砂吞,它是動態(tài)的署恍,也就是說當你打開這個網(wǎng)頁,只會同步下載對應的字體文件蜻直。所以每一次請求網(wǎng)頁的時候盯质,都要去下載這個字體文件,也就是字體對應表概而。
import requests
from bs4 import BeautifulSoup
import re
from urllib.request import urlretrieve
from fontTools.ttLib import TTFont
def get_response():
url = 'https://m.58.com/gz/qzyewu/pn1/?reform=pcfront'
headers = {
'authority': 'm.58.com',
'method': 'GET',
'path': '/gz/qzyewu/pn2/?reform=pcfront&PGTID=0d303353-0000-3f36-e12a-3e4c239050a4&ClickID=2&segment=true',
'scheme': 'https',
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cookie': 'f=n; id58=c5/njVw1zDFsj83TBA4MAg==; 58tj_uuid=10c6879a-760b-4754-afa1-0fb1f5b0fc12; als=0; __utma=253535702.1877351784.1547029555.1547029555.1548918015.2; __utmz=253535702.1548918015.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; city=sh; 58home=sh; wmda_uuid=5bcd52e27e1ca928a262958ae23846fe; wmda_new_uuid=1; wmda_visited_projects=%3B2385390625025; xxzl_deviceid=kaWnkw7KX8T2RCmSUWyrSm0%2BRTaHxr7dklX5it5enKJyoVP3jXCTPPbcNlqeMk7p; sessionid=a63b2622-6788-4db5-bfaf-836bf3b60c74; device=m; new_uv=4; qz_gdt=; utm_source=; spm=; init_refer=; scancat=13139; Hm_lvt_35e5bfb064bb1db376f3263abb7cfe79=1555137939; m58comvp=t13v115.159.229.24; hasLaunchPage=%7Cl_searchjob_qzyewu%7C; launchFlag=1; Hm_lvt_fb84137acd0fd92ca74ea0f89f207b1f=1555137939; f=n; new_session=0; cookieshow_13139=11; ppStore_fingerprint=96D8AAFDAD9596640390B38637943FB0CF146BCB8CD892C3%EF%BC%BF1555138230467; Hm_lpvt_35e5bfb064bb1db376f3263abb7cfe79=1555138232; Hm_lpvt_fb84137acd0fd92ca74ea0f89f207b1f=1555138233; JSESSIONID=2D33CDB828683462B5850797534E140F',
'referer': 'https://m.58.com/gz/qzyewu/pn1/?reform=pcfront&PGTID=0d303353-0000-3f36-e12a-3e4c239050a4&ClickID=2',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
response = requests.get(url, headers=headers)
return response
# 解析字體文件 唤殴,獲取相應的字體映射關系
def parse_font():
font1 = TTFont('58tc.woff')
keys, values = [], []
for k, v in font1.getBestCmap().items():
if v.startswith('uni'):
keys.append(eval("u'\\u{:x}".format(k) + "'"))
values.append(chr(int(v[3:], 16)))
else:
keys.append("&#x{:x}".format(k))
values.append(v)
return keys, values
# 獲取數(shù)據(jù)并對特殊字體轉碼
def get_data():
# 獲取內(nèi)容
response = get_response()
# 獲取woff文件
font_url = re.findall(r'src:url\((.*?)\)', response.text, re.S)[0]
urlretrieve(font_url, '58tc.woff')
# 解析內(nèi)容
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.findAll('dt',class_='tit')
for info in data:
title = str(info.text).strip()
keys, values = parse_font()
for k, v in zip(keys, values):
title = title.replace(k, v)
print(title)
if __name__ == '__main__':
get_data()