python爬虫--房产数据爬取并保存本地
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、插入标题的方式,数组的写入
最新文章
- zabbix解决中文乱码问题(没有测试成功)
- Linux下磁盘分区挂载
- JDBC操作数据库 封装好的工具类
- DLL ActiveForm 线程同步问题
- Apache MINA 框架之默认session管理类实现
- Hibernate命名空间怎样实现?
- Linux系统查看有几块硬盘
- 201521123022 《Java程序设计》 第九周学习总结
- 团队作业4——第一次项目冲刺(Alpha版本) Day 1
- python Josnp(跨域)
- JAVA 四舍五入Math.round方法
- flask 第七章 简陋版智能玩具 +MongoDB初识和基本操作
- IDEA集成git方法
- Java 接口 Closeable
- react 编写日历组件
- vue里的样式添加之类名改动 和style改动
- springboot学习之构建简单项目搭建
- too many open files(打开的文件过多)解决方法
- PHP查看编译参数
- 在VS代码中使用版本控制
热门文章
- K8存储之ConfigMap、Secret
- RIDE,如何指定report,log,output的存放位置
- TIM PC版 v3.4.5.22071 绿色便携版
- Nodejs杀死本地应用(win)
- c++练习267题 火柴棒等式
- springboot配置ssl变成https证书
- Oracle查询表中的各列的列名,数据类型,以及类型长度
- jumpserver运行源码
- LogAgent —— etcd+kafka+zookeeper+go实现实时读取日志发送到kafka,并实现热加载配置读取的日志路径
- 错误:[ERROR] 不再支持源选项 5。请使用 6 或更高版本。 [ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。