最近抓取了大众点评的评论,其中遇到了相当有趣的用图片代替字体的反扒方法,给大家分享一下。
抓取目的商铺的评论及相关信息
获取每个字代表的偏移量
def get_font_dict():
"""
获取每个字代表的偏移量
:return:
"""
url = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/c6763031f4ca5ef21c6b079409f076b9.css'
url = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/66ed5cd66492f2f67ccf37ae948a303c.css'
r = requests.get(url, headers=headers)
font_list = re.findall('.*?{.*?}', r.text)
font_dict = {}
for font in font_list:
# print(font)
class_font = font.split('{')[0].replace('.', '')
pianyi_list = re.findall('\d+', font.split('{')[1])
num_list = [int(x) for x in pianyi_list if int(x) != 0]
if len(num_list) == 1:
num_list.insert(0, 0)
font_dict[class_font] = num_list
# print(font_dict)
return font_dict
获取每一行的文字
def get_font_place():
"""
获取每一行对应的文字
:return:
"""
# url = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/3617c635ce7e39c7e8fa09bb3a0ea481.svg'
url = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/145a30db44df01339efea895999d3587.svg'
place_list = []
id_dict = []
r = requests.get(url, headers)
# print(r.text)
soup = BeautifulSoup(r.text, 'html.parser')
# print(soup)
text_list = soup.find_all('text')
# text_list = soup.find_all('textpath')
# id_list = soup.find_all('path')
# print(len(text_list))
for i in range(len(text_list)):
# x = text_list[i]['textlength']
# # st = text_list[i].text
# # # print(id_list[i])
# # y = id_list[i]['d'].split()[1]
# # # print(y, st)
# # place_list.append({y: [x, st]})
y = text_list[i]['y']
text = text_list[i].text
place_list.append({y:text})
# print(place_list)
return place_list
获取相应的位置对应的真实文字
def get_ture_font(place):
"""
获取真实的评论字
:param place:
:return:
"""
num = 0
for i in font_place:
for k in i:
if int(k) > place[1]:
# print(i)
num = 1
st = i[k]
# st = i[k][1]
# leng = i[k][0]
if num:
break
# st_num = int((int(leng) - place[0]) / 14)
st_num = int(place[0]) / 14
# print(st_num)
# true_font = st[len(st) - st_num]
true_font = st[int(st_num)]
# print(true_font)
return true_font
大众点评的反爬比较严重,对ip、账号的实行限制,对字体也进行反爬,而且字体反爬的链接、规则也会变化,是个进行爬虫技能检阅的不错的网站。下面是详细的代码:详细代码地址