python爬虫 爬取贝壳网中所有行政区内的二手房 将获取信息存于excle中--Excel文件 含分析
-- coding: utf-8 --
“”"
Created on Sat Feb 29 19:13:37 2020
@author: acliu
“”"
#获取房屋的基本信息(若干页)
#贝壳二手房北京房价
#https://bj.ke.com/ershoufang/
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 1 19:12:53 2020@author: acliu
"""import xlwt
import requests
from bs4 import BeautifulSoupheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',\"Host": "bj.ke.com",} # 将获取的信息保存到表格中
def save_info(content):workbook = xlwt.Workbook(encoding = 'ascii')worksheet = workbook.add_sheet('house info')style = xlwt.XFStyle() # 初始化样式font = xlwt.Font() # 为样式创建字体font.name = 'Times New Roman' font.bold = True # 黑体font.underline = True # 下划线font.italic = True # 斜体字style.font = font # 设定样式worksheet.write(0, 0, '名称')worksheet.write(0, 1, '位置')worksheet.write(0, 2, '房屋信息')worksheet.write(0, 3, '总价(万)')worksheet.write(0, 4, '单价(元/平方米)')for i, item in enumerate(content):for j in range(5): #多添加一列(序号)worksheet.write(i+1, j, content[i][j])workbook.save('./house_info.xls') # 保存文件# 获取房屋相关的信息
# 主要包括:title positon houseinfo totalprice unitprice
def get_info():all_info = []title_list = []position_list = []house_list = []totalPrice_list = []unitPrice_list = []for i in range(100):link = 'https://bj.ke.com/ershoufang/dongcheng/pg%dl2/' % ir = requests.get(link, headers=headers, timeout=10)print (str(i+1), 'status_code: ', r.status_code)soup = BeautifulSoup(r.text, 'lxml')titleInfo = soup.findAll('div', {'class': 'info clear'})positionInfo = soup.findAll('div', {'class': 'positionInfo'})houseInfo = soup.findAll('div', {'class': 'houseInfo'})totalPrice = soup.findAll('div', {'class': 'totalPrice'})unitPrice = soup.findAll('div', {'class': 'unitPrice'})for item in titleInfo:title = item.div.a.text.strip()title_list.append(title)for item in positionInfo:postion = item.a.text.strip()position_list.append(postion)for item in houseInfo:house = item.text.strip().replace('\n', ' ').replace(' ', '')house_list.append(house)for item in totalPrice:total_price = item.span.text.strip()totalPrice_list.append(total_price)for item in unitPrice:unit_price = item.span.text.strip().replace('单价', '').replace('元/平米', '')unitPrice_list.append(unit_price)print (len(title_list))print (len(position_list))print (len(house_list))print (len(totalPrice_list))print (len(unitPrice_list))for i in range(len(title_list)):item = [title_list[i], position_list[i], house_list[i], totalPrice_list[i], unitPrice_list[i]]all_info.append(item)return all_infoif __name__ == "__main__":all_info = get_info()save_info(all_info)
python爬虫 爬取贝壳网中所有行政区内的二手房 将获取信息存于excle中--Excel文件 含分析相关推荐
- python爬虫爬取知网
python爬虫爬取知网 话不多说,直接上代码! import requests import re import time import xlrd from xlrd import open_wor ...
- 在当当买了python怎么下载源代码-python爬虫爬取当当网
[实例简介]python爬虫爬取当当网 [实例截图] [核心代码] ''' Function: 当当网图书爬虫 Author: Charles 微信公众号: Charles的皮卡丘 ''' impor ...
- python爬虫爬取当当网的商品信息
python爬虫爬取当当网的商品信息 一.环境搭建 二.简介 三.当当网网页分析 1.分析网页的url规律 2.解析网页html页面 书籍商品html页面解析 其他商品html页面解析 四.代码实现 ...
- [python爬虫]爬取天气网全国所有县市的天气数据
[python爬虫]爬取天气网全国所有县市的天气数据 访问URL 解析数据 保存数据 所要用到的库 import requests from lxml import etree import xlwt ...
- python爬虫爬取慕课网中的图片
我们简单地爬取慕课网中免费课程下的第一页的图片,如想爬取多页图片,可以添加for循环自行实现 python版本:3.6.5 爬取网址:http://www.imooc.com/course/list ...
- Python爬虫爬取东方财富网的股票信息
简单的Python爬虫应用 目标:爬取东方财富网的股票信息 1.先找到要爬取的网页 2.第二步开始爬取信息 2.1 通过requests获取网页信息 2.2再通过BeautifulSoup解析: 2. ...
- python爬虫爬取东方财富网股票走势+一些信息
一.目标 我们的目标是爬取东方财富网(https://www.eastmoney.com/)的股票信息 我的目标是爬取100张股票信息图片 经过实际测试我的爬取范围为000001-000110,000 ...
- [Python爬虫]爬取东方财富网公司公告需要注意的几个问题
在上一篇文章中,以爬取东方财富网公司公告为例,介绍了如何爬取利用ajax加载的网页,以及如何模拟翻页.但是,在实际应用的过程中,发现了一些问题.接下来就来一一分析解决. 问题一.公告数量过多,后面的公 ...
- python爬虫-爬取当当网书籍信息存到Excel中
文章目录 一.任务 二.分析 (一).单页面的信息分析 源代码分析 目标信息定位与分析 代码设计 (二).所有目标页面链接分析 目标链接分析 代码设计 三.注意要点 四.完整代码 五.参考 一.任务 ...
最新文章
- 宋琦:PHP在微博优化中的“大显身手”
- docker oracle创建实例_五分钟教你搭建oracle数据库环境
- Plasma链0x1的构造
- 图片碎片化mask动画
- 分析约瑟夫问题(循环单链表)
- Flex CursorManager
- 如何在源代码(而非库文件)模式下构建并且运行 SAP Spartacus
- 万兆以太网测试仪应该具备什么功能
- 读写自旋锁,第1部分(来自IBM)
- 广电总局清查整治电视购物频道 23档收藏类购物节目停播
- centos环境安装storm
- 手把手教你自制一寸两寸照
- Ubuntu下VSCode使用等宽字体设置
- 浏览器h5拉起app 微信拉起app wx-launch-open-app
- 单生狗必备之如何用Python给PLMM表白
- 中规中矩的linux安装redis
- 不确定性的人生,其实是有确定性的东西
- Algorand正式开放测试网TestNet!
- 搭建B2B电子商务系统要怎么做
- ckeditor 使用说明,手把手,详细步骤: ckeditor-5 online-builder,持续更新中~~