一、分析
1、爬取网页中的租金发现,数字是由数字背景图片中的偏移量对应的数值
2、获取源码
3、保存图片
4、解析图片中的数字
5、通过计算得到每一位数字对应的偏移量所组成的字典类型数据
一个位置的偏移量是21.4,具体数字的位置为:
x_list = ['-0px', '-21.4px', '-42.8px', '-64.2px', '-85.6px',
'-107px', '-128.4px', '-149.8px', '-171.2px','-192.6px']
6、获取网页源码中价格对应的偏移量来进行替换成对应的数字
二、程序实现
import pytesseract
import requests
from lxml import etree
import re
from urllib import request
class ZiRoom():
# 获取网页源码和保存数字背景图片
def get_html_img(self):
url = 'https://www.ziroom.com/z/'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}
res = requests.get(url, headers=header).text
# 先用xpath初步定位到url的位置
html = etree.HTML(res)
back_img_info = html.xpath('//span[@class="num"]/@style')[0]
# https://static8.ziroom.com/phoenix/pc/images/price/new-list/dff9d441e1fc59f793d5c3b68461b3ea.png
# 再用正则具体匹配
back_img_url = 'https:' + re.search(r'url\((.*?)\)', back_img_info).group(1)
request.urlretrieve(back_img_url, 'back_img_url.png') # 保存图片
# 返回源码
return html
# 解析标题和价格
def repalce_data(self, res, replace_dict):
div_list = res.xpath('//div[@class="Z_list-box"]/div[@class="item"]')
for div in div_list:
title = div.xpath('.//h5[contains(@class, "title")]/a/text()')[0]
print("房源:",title)
# 获取价格
style_list = div.xpath('.//div[@class="price "]/span[@class="num"]/@style')
# 用拼接字符串的方式得到最终价格
price = ''
for style in style_list:
position = style.split(': ')[-1]
price = price + replace_dict[position]
print("月租:",price)
def main(self):
res = self.get_html_img()
# 识别图片中的数字
number_str = pytesseract.image_to_string("./back_img_url.png")
# num_list = [6, 9, 4, 8, 5......]
# 真实数字列表
number_list = re.findall('\d', number_str)
# 偏移量列表
x_list = ['-0px', '-21.4px', '-42.8px', '-64.2px', '-85.6px', '-107px', '-128.4px', '-149.8px', '-171.2px','-192.6px']
# ['6', '2', '9', '1', '0', '7', '8', '4', '5', '3']
# replace_dict 有替换关系的字典
replace_dict = dict(zip(x_list, number_list))
# {'-0px': '8', '-21.4px': '6', '-42.8px': '7', '-64.2px': '0', '-85.6px': '4', '-107px': '1', '-128.4px': '5', '-149.8px': '9', '-171.2px': '2', '-192.6px': '3'}
self.repalce_data(res, replace_dict)
if __name__ == '__main__':
room = ZiRoom()
room.main()
图片地址:https://static8.ziroom.com/phoenix/pc/images/price/new-list/48d4fa3aa7adf78a1feee05d78f33700.png
房源: 合租·鸿顺园东区5居室-南卧
月租: 1760
房源: 城市之光丽泽店·一居博学
月租: 2699
房源: 整租·运河明珠2居室-南
月租: 5460
房源: 合租·首开熙悦睿府书香4居室-南卧
月租: 1830
房源: 合租·金汉绿港三区4居室-南卧
月租: 2090
房源: 合租·西辛南区3居室-南卧
月租: 2360
房源: 合租·东兴一区3居室-南卧
月租: 2260
房源: 合租·碧波园3居室-南卧
月租: 1890
房源: 整租·紫运南里一区2居室-南
月租: 5530
房源: 合租·幸福西区3居室-南卧
月租: 2030
房源: 合租·建邦华庭东区3居室-南卧
月租: 2460
房源: 合租·煤炭公司家属楼3居室-南卧
月租: 2390
房源: 合租·首创紫悦台4居室-南卧
月租: 2490
房源: 合租·前进花园牡丹苑3居室-南卧
月租: 2460
房源: 合租·鸿顺园东区3居室-南卧
月租: 1790
房源: 合租·天时名苑4居室-南卧
月租: 2760
房源: 合租·稻田回迁楼北区3居室-南卧
月租: 2290
房源: 合租·清源西里3居室-南卧
月租: 2430
房源: 合租·碧波园3居室-南卧
月租: 2160
房源: 合租·前进花园石门苑3居室-南卧
月租: 2330
房源: 合租·中国铁建青秀尚城3居室-南卧
月租: 2460
房源: 合租·保利春天里5居室-南卧
月租: 2360
房源: 合租·路劲世界城二期3居室-南卧
月租: 2390
房源: 整租·新光大中心1居室-北
月租: 6930
房源: 合租·幸福西区3居室-南卧
月租: 2260
房源: 合租·芭蕾雨悦都南区3居室-南卧
月租: 2360
房源: 合租·车站北里3居室-南卧
月租: 2860
房源: 合租·世纪东方城4居室-南卧
月租: 3160
房源: 合租·龙跃苑三区6居室-南卧
月租: 2990