#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…’)