python爬取房源信息并存储到

#coding:utf-8
from bs4 import BeautifulSoup
from urlparse import urljoin
import requests
import csv
import html5lib
URL = ‘http://sh.ganji.com/fang1/’#爬的目标
ADDR = ‘http://sh.ganji.com/’#赶集网首页

if name == ‘main’:
start_page = 1
end_page = 10
price = 7#价格为7000左右
with open(‘ganji.csv’,‘wb’) as f:#创建一个csv文件
csv_writer = csv.writer(f,delimiter = ‘,’)#以逗号隔开在f文件里写出来
print(‘start…’)
while start_page <= end_page:
start_page += 1
print(‘get:{0}’.format(URL.format(page = start_page,price= price)))
response = requests.get(URL.format(page = start_page,price = price))
html = BeautifulSoup(response.text,‘html.parser’)
#第一个参数是要抓取的html文本,第二个是使用哪种解析器
house_list = html.select(’.f-list > .f-list-item > .f-list-item-wrap’)
#获取房源信息
if not house_list:#获取不到房源信息则跳出
break
for house in house_list:
house_title = house.select(’.title > a’)[0].string.encode(‘utf-8’)
house_addr = house.select(’.address > .area > a’)[-1].string.encode(‘utf-8’)
house_price = house.select(’.info > .price > .num’)[0].string.encode(‘utf-8’)
house_url = urljoin(ADDR,house.select(’.title > a’)[0][‘href’])
csv_writer.writerow([house_title,house_addr,house_price,house_url])
print(‘end…’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值