import requests
import csv
from bs4 import BeautifulSoup
headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.6.1000'}
for i in range(1,10):
link='https://fz.anjuke.com/sale/p'+str(i)+'/#filtersort'
r=requests.get(link,headers=headers)
print(str(i + 1), "页响应状态码:", r.status_code)
soup=BeautifulSoup(r.text,'lxml')
house_list=soup.find_all('li',class_="list-item")
with open('test.csv', 'a',newline='',encoding='utf-8-sig')as csvfile:
w=csv.writer(csvfile)
w.writerow(('标题','价格','均价','面积','楼层'))
for house in house_list:
temp = []
name=house.find('div',class_='house-title').a.text.strip()
price=house.find('div',class_='pro-price').contents[1].text.strip()
price_ave=house.find('div',class_='pro-price').contents[2].text.strip()
area=house.find('div',class_='details-item').span.text
floor=house.find('div',class_='details-item').contents[5].text
temp=[name,price,price_ave,area,floor]
print(temp)
w.writerow(temp)

 

几个注意点:

1、with open('test.csv', 'a',newline='',encoding='utf-8-sig')as csvfile:,注意utf8转码,否则数据保存本地会为乱码形式

2、插入标题的方式,数组的写入

最新文章

  1. zabbix解决中文乱码问题(没有测试成功)
  2. Linux下磁盘分区挂载
  3. JDBC操作数据库 封装好的工具类
  4. DLL ActiveForm 线程同步问题
  5. Apache MINA 框架之默认session管理类实现
  6. Hibernate命名空间怎样实现?
  7. Linux系统查看有几块硬盘
  8. 201521123022 《Java程序设计》 第九周学习总结
  9. 团队作业4——第一次项目冲刺(Alpha版本) Day 1
  10. python Josnp(跨域)
  11. JAVA 四舍五入Math.round方法
  12. flask 第七章 简陋版智能玩具 +MongoDB初识和基本操作
  13. IDEA集成git方法
  14. Java 接口 Closeable
  15. react 编写日历组件
  16. vue里的样式添加之类名改动 和style改动
  17. springboot学习之构建简单项目搭建
  18. too many open files(打开的文件过多)解决方法
  19. PHP查看编译参数
  20. 在VS代码中使用版本控制

热门文章

  1. K8存储之ConfigMap、Secret
  2. RIDE,如何指定report,log,output的存放位置
  3. TIM PC版 v3.4.5.22071 绿色便携版
  4. Nodejs杀死本地应用(win)
  5. c++练习267题 火柴棒等式
  6. springboot配置ssl变成https证书
  7. Oracle查询表中的各列的列名,数据类型,以及类型长度
  8. jumpserver运行源码
  9. LogAgent —— etcd+kafka+zookeeper+go实现实时读取日志发送到kafka,并实现热加载配置读取的日志路径
  10. 错误:[ERROR] 不再支持源选项 5。请使用 6 或更高版本。 [ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。