python 爬取自如租房的租房数据

完整代码下载:https://github.com/tanjunchen/SpiderProject/tree/master/ziru

#!/usr/bin/python
# -*- coding: UTF-8 -*-import re
import requests
import pytesseract
from PIL import Image
from selenium import webdriver
from fake_useragent import UserAgent
from lxml import etree
from urllib import parse
import pandas as pd
from datetime import datetimeua = UserAgent()
headers = {"User-Agent": ua.random,"Referer": "http://gz.ziroom.com/"}class ZiRoom(object):def __init__(self):self.driver = webdriver.Chrome()self.all_data = []def get_content(self, name):div_list = self.driver.find_elements_by_xpath('//*[@id="houseList"]/li')number = self.get_image_number()print(number)for div in div_list[1:]:try:price_list = []# 如果网页中的值不存在 则可能会存在部分数据丢失 也就是空数据丢失 基本不会有什么影响for i in range(2, 6):start_price = \div.find_element_by_xpath('.//div[3]/p/span[{}]'.format(i)).get_attribute('style').split(' ')[1].replace('-', '').replace('px', '')price = number[int(int(start_price) / 30)]price_list.append(price)price = '{}元/每月'.format(''.join(price_list))title = div.find_element_by_xpath('.//div[2]/h3/a').text.replace(' ', '')location = div.find_element_by_xpath('.//div[2]/div//p[2]/span').text.replace(' ', '')area = div.find_element_by_xpath('.//div[2]/div/p/span').text.replace(' ', '')self.all_data.append([name, title, price, location, area])except BaseException as e:print(e)passdef get_image_number(self):'''获取价格的图片  并且通过 pytesseract 识别图片 解成数字列表:return:'''# 价格数据来源于js 加载的图片html = self.driver.execute_script("return document.documentElement.outerHTML")photo = re.findall('var ROOM_PRICE = {"image":"(//.*?.png)"', html)[0]image = requests.get('http:' + photo).contentf = open('price.png', 'wb')f.write(image)f.close()num = []'''pytesseract psm 选项参数0    Orientation and script detection (OSD) only.1    Automatic page segmentation with OSD.2    Automatic page segmentation, but no OSD, or OCR.3    Fully automatic page segmentation, but no OSD. (Default)4    Assume a single column of text of variable sizes.5    Assume a single uniform block of vertically aligned text.6    Assume a single uniform block of text.7    Treat the image as a single text line.8    Treat the image as a single word.9    Treat the image as a single word in a circle.10    Treat the image as a single character. 11    Sparse text. Find as much text as possible in no particular order.12    Sparse text with OSD.13    Raw line. Treat the image as a single text line'''number = pytesseract.image_to_string(Image.open("price.png"),config="-psm 8 -c tessedit_char_whitelist=1234567890")for i in number:num.append(i)return numdef run(self):url = "http://www.ziroom.com/z/nl/z2-d23008614.html"res = requests.get(url, headers=headers)res.encoding = "utf-8"html = etree.HTML(res.text)name = html.xpath("//div[@class='selection_con']/dl[2]/dd/ul/li[position()>1]/span/a/text()")urls = html.xpath("//div[@class='selection_con']/dl[2]/dd/ul/li[position()>1]/span/a/@href")urls = [parse.urljoin(url, n) for n in urls]data = zip(name, urls)for k, v in data:print("正在获取", k, v)res = requests.get(v, headers=headers)res.encoding = "utf-8"html = etree.HTML(res.text)page_xpath = html.xpath("//span[@class='pagenum']/text()")if page_xpath:page_num = page_xpath[0].strip("/")if page_num:for i in range(1, int(page_num) + 1):# self.driver.implicitly_wait(10)self.driver.get(v + '?p={}'.format(i))self.get_image_number()self.get_content(k)df = pd.DataFrame(self.all_data)df.columns = ['地区', '房源', '价格', '位置', '面积']df.to_csv("all_result_" + datetime.now().strftime('%Y-%m-%d') + ".csv", index=False, encoding="gbk")if __name__ == '__main__':z = ZiRoom()z.run()

python 爬取自如租房的租房数据,使用图像识别获取价格信息相关推荐

  1. python 爬取自如网租房信息(解决照片价格问题)

    一.背景 爬取自如网的租房信息时,本来以为是手到擒来的事,结果却被租房价格卡死了.有脾气的自如,价格居然搞成了照片,并且照片是乱序的0到9,每次根据像素位截取. 最气人的,还是一张照片只取一个数字,例 ...

  2. 用python爬取网站_「自如网」关于用python爬取自如网信息的价格问题(已解决) - seo实验室...

    自如网 ###这是一篇求助文,我能获取图片并变成字符串,但是无法获取位移量### 前两坛突发奇想想要爬取自如网的租房数据,本来以为能够请求+美丽+ re能全部搞定,没想到这个网站的反爬机制有点让我搞不 ...

  3. 关于用python爬取自如网信息的价格问题(已解决)

    ###这是一篇求助文,我能获取图片并变成字符串,但是无法获取位移量### 前两坛突发奇想想要爬取自如网的租房数据,本来以为能够请求+美丽+ re能全部搞定,没想到这个网站的反爬机制有点让我搞不定先贴个 ...

  4. python爬取自如房间信息(一)

    使用python和selenium+Chrome Headless爬取自如房间信息,并将结果存储在MongoDB中.其中最麻烦的应该是每间房的价格,因为自如是用一张图片和offset来显示价格,所以不 ...

  5. 练习:使用Python爬取COVID-19疫情国内当日数据

    练习:使用Python爬取COVID-19疫情国内当日数据 推荐公众号:数据酷客 (里面有超详细的教程) 代码来源数据酷客公众号教程 URL它是Uniform Resource Locator的缩写, ...

  6. python爬取股票信息_利用Python爬取网易上证所有股票数据(代码

    利用Python爬取网易上证所有股票数据(代码 发布时间:2018-04-14 17:30, 浏览次数:1261 , 标签: Python import urllib.request import r ...

  7. 如何使用python编程抢京东优惠券 知乎_学好Python爬取京东知乎价值数据

    原标题:学好Python爬取京东知乎价值数据 Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫.学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方面,像 Python这 ...

  8. python如何爬取实时人流量_使用python爬取微信宜出行人流量数据

    代码地址:https://liujiao111.github.io/2019/06/18/easygo/ 工具介绍: 该工具基于微信中的宜出行提供的数据接口进行爬取,能够爬取一定范围内的当前时间点的人 ...

  9. 使用python爬取BOSS直聘岗位数据并做可视化(Boss直聘对网页做了一些修改,现在的代码已经不能用了)

    使用python爬取BOSS直聘岗位数据并做可视化 结果展示 首页 岗位信息 岗位详情 薪资表 学历需求 公司排名 岗位关键词 福利关键词 代码展示 爬虫代码 一.导入库 二.爬取数据 1.爬取数据代 ...

最新文章

  1. 向深度学习三剑客学习四种科研精神(上)
  2. 旷视砸20亿进军AIoT,发布国内首个机器人协作大脑河图
  3. C++11特性(模板类 initializer_list)
  4. 【Tools】CSDN中如何添加数学公式
  5. Open3D编译安装
  6. c# 解析gprmc数据_Windows下VLP16激光雷达数据解析
  7. android中如何执行java命令
  8. 阿里巴巴发布招聘微博:新财年新增超过1800岗位需求
  9. OpenShift 4 - 用CA证书或Token访问Internal Registry中的容器
  10. uva 550 有趣的乘法(dfs)
  11. 节 海里/小时 千米/小时 米/秒 转换
  12. 数据库篇之[bsp_advertpositions]数据表-BrnShop1.9升级至2.1升级说明(非官方版本)...
  13. debian软raid
  14. 百度云盘资源下载器分享【只要有分享地址+提取码】
  15. cdrx8如何批量导出jpg_Coreldraw/CDR X8 存低版本打开问题 – 数码打印破图 – Coreldraw/CDR软件崩溃 – 渐变导位图角度变了...
  16. apicloud访问php,支付宝 app应用 受权 php + APICloud
  17. 叉积 微分 恒等式_不等式(O):常见符号及恒等式
  18. mysql日期教程_Navicat for MySQL 设置日期时间栏位教程(图文)
  19. 教你免费将手机里的PDF转Word还能同步到电脑
  20. NVIDIA GPU 计算能力

热门文章

  1. matlab normpdf用法,matlab-ksdensity用法
  2. 微信之夜,张小龙说视频化表达将会成为下一个十年内容领域的主题
  3. Android底部导航栏的四种实现
  4. 知乎代运营经验:知乎问答怎么运营,怎么增粉?
  5. [转]C++ 堆栈溢出的原因以及可行的解决方法
  6. 前端复习 css(一)
  7. 普宁跨境电商外贸 阿里国际 之100%点击的开发信标题
  8. 解决Hash冲突的方法
  9. excel利用公式取最后一行单元格内容
  10. java家用电器springboot家电销售网站管理系统