用python爬取链家二手房信息,并把数据存入mongodb 旧城等待, 2023-02-11 04:42 34阅读 0赞 爬取网站:[https://gz.lianjia.com/ershoufang/pg][https_gz.lianjia.com_ershoufang_pg] 爬取链家二手房信息,数据存mongodb,代码如下: import requests from fake_useragent import UserAgent from lxml import etree import time import random from pymongo import MongoClient class LianjiaSpider(object): def __init__(self): self.url = 'https://gz.lianjia.com/ershoufang/pg{}/' # 获取页面,异常处理 def get_html(self, url): ua = UserAgent() headers = { 'User-Agent': ua.random} response = requests.get(url=url, headers=headers) return response.content.decode('utf-8') # 解析页面 def parse_html(self, url): html = self.get_html(url) # with open('test.html', 'w', encoding='utf-8') as f: # f.write(html) parse_obj = etree.HTML(html) li_list = parse_obj.xpath('//ul[@class="sellListContent"]/li') # 2.for循环一次遍历每个房源li节点,获取1房源的所有注释 house_list = [] item = { } for li in li_list: # 名称[0]:第一个房源,strip移除空格 item['name'] = li.xpath('.//div[@class="positionInfo"]/a[1]/text()')[0].strip() # 信息 户型+面积+方向+装修+楼层+时间+类型 # 2室1厅 | 51.99平米 | 西 | 精装 | 中楼层(共18层) | 2000年建 | 塔楼 info_list = li.xpath('.//div[@class="houseInfo"]/text()')[0].split('|') item['model'] = info_list[0].strip() item['area'] = info_list[1].strip() item['direction'] = info_list[2].strip() item['perfect'] = info_list[3].strip() item['floor'] = info_list[4].strip() item['time'] = info_list[5].strip()[:-2] item['type'] = info_list[-1].strip() if len(info_list) == 6 else None # 地区 总价 单价 item['address'] = li.xpath('.//div[@class="positionInfo"]/a[2]/text()')[0].strip() item['total price'] = li.xpath('.//div[@class="totalPrice"]/span/text()')[0].strip() item['unit price'] = li.xpath('.//div[@class="unitPrice"]/span/text()')[0].strip() house_list.append(item) lianjia_info = dict() lianjia_info['url'] = self.url lianjia_info['描述'] = '链家二手房信息' lianjia_info['信息'] = house_list return lianjia_info def save_data(self, data): client = MongoClient('mongodb://localhost:27017') collection = client["student_de"] collection['class_one'].insert(data) def run(self): for pg in range(1, 3): url = self.url.format(pg) data = self.parse_html(url) self.save_data(data) time.sleep(random.randint(1, 3)) if __name__ == '__main__': spider = LianjiaSpider() spider.run() 执行成功,结果如下图: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1aV95b25naHVh_size_16_color_FFFFFF_t_70] [https_gz.lianjia.com_ershoufang_pg]: https://gz.lianjia.com/ershoufang/pg [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1aV95b25naHVh_size_16_color_FFFFFF_t_70]: https://img-blog.csdnimg.cn/2020052119393620.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1aV95b25naHVh,size_16,color_FFFFFF,t_70
相关 python爬虫之静态网页爬取--猫眼电影/链家二手房 猫眼电影(xpath) 目标 1、地址: 猫眼电影 - 榜单 - top100榜 2、目标: 电影名称、主演、上映时间 步骤 本是古典 何须时尚/ 2023年06月22日 08:25/ 0 赞/ 124 阅读
相关 爬取链家任意城市二手房数据(天津) 1 !/usr/bin/env python 2 -- coding: utf-8 -- 3 @Time : 2019-08-16 1 桃扇骨/ 2023年06月03日 04:00/ 0 赞/ 107 阅读
相关 用python爬取链家二手房信息,并把数据存入mongodb 爬取网站:[https://gz.lianjia.com/ershoufang/pg][https_gz.lianjia.com_ershoufang_pg] 爬取链家二手房 旧城等待,/ 2023年02月11日 04:42/ 0 赞/ 35 阅读
相关 Python爬虫新手入门教学(三):爬取链家二手房数据 前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 Python爬虫、数据分析、网站开发等案例教程视频免费在线观 港控/mmm°/ 2023年01月11日 03:44/ 0 赞/ 297 阅读
相关 Python爬虫入门教程03:二手房数据爬取 前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 前文内容 [Python爬虫入门教程01:豆瓣Top电 素颜马尾好姑娘i/ 2023年01月10日 10:26/ 0 赞/ 293 阅读
相关 python爬取链家二手房信息,确认过眼神我是买不起的人 前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 淡淡的烟草味﹌/ 2022年12月08日 14:19/ 0 赞/ 257 阅读
相关 Python爬虫之链家二手房数据爬取 Python 依赖模块: requests parsel csv 功能要求: ![watermark_type_ZHJvaWRzYW5zZmFs 本是古典 何须时尚/ 2022年09月11日 07:23/ 0 赞/ 455 阅读
相关 Python爬取城市二手房数据 今天要分享的教程是爬取各大城市的二手房数据,抛开以前的文章不谈,下面的内容应该足够你实现这篇爬虫。以下是正文: 1. 确定目标 今天我们的目标官网链接是:https:/ ﹏ヽ暗。殇╰゛Y/ 2022年08月29日 14:55/ 0 赞/ 370 阅读
相关 爬取链家网所有二手房楼盘信息 代码如下: import requests from lxml import etree import math import time 缺乏、安全感/ 2022年02月04日 17:13/ 0 赞/ 504 阅读
还没有评论,来说两句吧...