python爬取链家数据_Python爬取链家北京二手房数据

本文介绍了一个使用Python Scrapy和requests库爬取链家北京二手房数据的实例。通过模拟浏览器发送请求,解析HTML内容,获取房源的地点、拼音、社区、户型、面积、关注数、浏览数、发布时间、价格、平均价格等关键信息。
摘要由CSDN通过智能技术生成

# -*- coding: utf-8 -*-

import scrapy

import requests

import re

import time

from lxml import etree

from ..items import LianjiaItem

from scrapy_redis.spiders import RedisSpider

classLianjiaSpider(RedisSpider):

name = 'lianjiaspider'

redis_key = 'lianjiaspider:urls'

start_urls = 'http://bj.lianjia.com/ershoufang/'

defstart_requests(self):

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22\Safari/537.36 SE 2.X MetaSr 1.0'

headers = {'User-Agent': user_agent}

yield scrapy.Request(url=self.start_urls, headers=headers, method='GET', callback=self.parse)

defparse(self,response):

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22\Safari/537.36 SE 2.X MetaSr 1.0'

headers = {'User-Agent': user_agent}

lists = response.body.decode('utf-8')

selector = etree.HTML(lists)

area_list = selector.xpath('/html/body/div[3]/div[2]/dl[2]/dd/div[1]/div/a')

for area in area_list:

try:

area_han = area.xpath('text()').pop() # 地点

area_pin = area.xpath('@href').pop().split('/')[2] # 拼音

area_url = 'http://bj.lianjia.com/ershoufang/{}/'.format(area_pin)

print(area_url)

yield scrapy.Request(url=area_url, headers=headers, callback=self.detail_url, meta={"id1":area_han,"id2":area_pin} )

except Exception:

pass

defget_latitude(self,url): # 进入每个房源链接抓经纬度

p = requests.get(url)

contents = etree.HTML(p.content.decode('utf-8'))

latitude = contents.xpath('/ html / body / script[19]/text()').pop()

time.sleep(3)

regex = '''resblockPosition(. )'''

items = re.search(regex, latitude)

content = items.group()[:-1] # 经纬度

longitude_latitude = content.split(':')[1]

return longitude_latitude[1:-1]

defdetail_url(self,response):

'http://bj.lianjia.com/ershoufang/dongcheng/pg2/'

for i in range(1,101):

url = 'http://bj.lianjia.com/ershoufang/{}/pg{}/'.format(response.meta["id2"],str(1))

time.sleep(2)

try:

contents = requests.get(url)

contents = etree.HTML(contents.content.decode('utf-8'))

houselist = contents.xpath('/html/body/div[4]/div[1]/ul/li')

for house in houselist:

try:

item = LianjiaItem()

item['title'] = house.xpath('div[1]/div[1]/a/text()').pop()

item['community'] = house.xpath('div[1]/div[2]/div/a/text()').pop()

item['model'] = house.xpath('div[1]/div[2]/div/text()').pop().split('|')[1]

item['area'] = house.xpath('div[1]/div[2]/div/text()').pop().split('|')[2]

item['focus_num'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[0]

item['watch_num'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[1]

item['time'] = house.xpath('div[1]/div[4]/text()').pop().split('/')[2]

item['price'] = house.xpath('div[1]/div[6]/div[1]/span/text()').pop()

item['average_price'] = house.xpath('div[1]/div[6]/div[2]/span/text()').pop()

item['link'] = house.xpath('div[1]/div[1]/a/@href').pop()

item['city'] = response.meta["id1"]

self.url_detail = house.xpath('div[1]/div[1]/a/@href').pop()

item['Latitude'] = self.get_latitude(self.url_detail)

except Exception:

pass

yield item

except Exception:

pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值