import os.path

import requests
from lxml import etree
import xlwt
import xlrd def create_excel():
if not os.path.exists('./lianjia_excel.xls'):
book = xlwt.Workbook(encoding='utf-8', style_compression=0) sheet = book.add_sheet(f'{city}-链家二手房', cell_overwrite_ok=True)
col = ('房源名称', '所在小区', '所在地区', '房源信息', '发布时间', '房源标签', '房源价格', '房源单价')
# 创建列名
for i in range(0, 8):
sheet.write(0, i, col[i])
savepath = './lianjia_excel.xls'
book.save(savepath)
return book
book = xlrd.open_workbook('./lianjia_excel.xls') # 得到文件
return book if __name__ == '__main__':
city = input('请输入需要查询的城市数据:')
# 创建一个excel
book = xlwt.Workbook(encoding='utf-8', style_compression=0) sheet = book.add_sheet(f'{city}-链家二手房', cell_overwrite_ok=True)
col = ('房源名称', '所在小区', '所在地区', '房源信息', '发布时间', '房源标签', '房源价格', '房源单价')
# 创建列名
for i in range(0, 8):
sheet.write(0, i, col[i]) url = 'https://cd.lianjia.com/ershoufang/'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
} page = requests.get(url=url, headers=headers)
page.encoding = 'utf-8'
tree = etree.HTML(page.text)
li_list = tree.xpath('//div[@class="content "]/div/ul[@class="sellListContent"]/li')
data_list = []
for li in li_list: li_div = li.xpath('./div[@class="info clear"]')
if len(li_div) <= 0:
continue
li_div = li.xpath('./div[@class="info clear"]')[0]
# 房源名称
li_title = li_div.xpath('./div[@class="title"]/a/text()')[0] # 房源链接
li_url = li_div.xpath('./div[@class="title"]/a/@href')[0]
# 所在小区
li_xiaoqu = li_div.xpath('./div[@class="flood"]/div/a[1]/text()')[0]
# 所在小区URL
li_xiaoqu_url = li_div.xpath('./div[@class="flood"]/div/a[1]/@href')[0]
# 所在地区
li_diqu = li_div.xpath('./div[@class="flood"]/div/a[2]/text()')[0]
# 所在地区URL
li_diqu_url = li_div.xpath('./div[@class="flood"]/div/a[2]/@href')[0]
# 房源信息
li_houseinfo = li_div.xpath('./div[@class="address"]/div/text()')[0]
# 发布时间
li_followinfo = li_div.xpath('./div[@class="followInfo"]//text()')
# 房源标签
li_tag = li_div.xpath('./div[@class="tag"]//text()')
# 房源总价
li_price = li_div.xpath('./div[@class="priceInfo"]/div[1]/span/text()')[0]
li_price = li_price + '万'
# 房源单价
li_unitPrice = li_div.xpath('./div[@class="priceInfo"]/div[2]/span/text()')[0]
data_list.append([li_title, li_xiaoqu, li_diqu, li_houseinfo, li_followinfo, li_tag, li_price, li_unitPrice]) for data in data_list:
for i in range(0, len(data_list)):
data = data_list[i]
for j in range(0, 8):
sheet.write(i + 1, j, data[j])
savepath = './lianjia_excel.xls'
book.save(savepath)

excel保存效果如图:

最新文章

  1. YUV格式介绍
  2. jQuery代码节选(事件)
  3. JNDI解读(转)
  4. [deviceone开发]-do_Viewshower的动画效果示例
  5. JSP页面中的pageEncoding和contentType两种属性
  6. JSP MVC
  7. for循环绑定事件,闭包思想!
  8. LuaFileSystem学习心得
  9. sql server返回插入数据表的id,和插入时间
  10. [面试题] BloomFilter 无序40亿不重复 uint 整数, 给予任意的数,求是否在这40亿之中 + 无序数组中找2个相同的值
  11. button变成href (即按钮超链效果)
  12. C语言strtok()函数:字符串分割
  13. Unity 的ICallHander在C#项目中实现注入
  14. Python新式类继承的C3算法
  15. 《C++标准程序库》学习笔记(一)C++相关特性
  16. 【组合&amp;取补集】数三角形 @CQOI2014/BZOJ3505/upcexam3843
  17. ABP框架系列之十七:(Data-Filters-数据过滤)
  18. 【SqlServer】Sqlserver中的DOS命令操作
  19. org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column &#39;alarmGroup&#39; from result set. Cause: java.sql.SQLException: Error
  20. 廖雪峰Java-3流程控制-7for循环

热门文章

  1. typora配置图床
  2. excel空格处理
  3. Arm-Linux子系统的互相Notify
  4. 【rust】rsut基础:模块的使用一、mod 关键字、mod.rs 文件的含义等
  5. docker-01基本介绍
  6. 一款备受欢迎的用户脚本管理器插件TampermonKey-油猴脚本管理器安装与使用
  7. Html音频播放代码
  8. .NET WebAPI 跨域问题(has been blocked by CORS policy:No Access-Control-Allow-Ogigin header is present on the requested resource)
  9. 微信小程序【关于地址信息的接入以及自动选择当前位置】
  10. 【TS】基础类型