...

import requests
from requests import ConnectionError
from bs4 import BeautifulSoup
import pymongo #MongoDB的配置
MONGO_URL = 'localhost'
MONGO_DB = 'Fangtianxia'
MONGO_TABLE = 'HouseInfo'
#配置MongoDB
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB] base_url = 'http://esf.sz.fang.com/housing/__1_0_0_0_'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'
} #发送请求,获得响应
def get_html(url):
print('正在爬取',url)
try:
response = requests.get(url=url,headers=headers)
if response.status_code == 200:
return response.text
else:
print(response.status_code)
return None
except ConnectionError:
print('出现错误!')
return None #解析响应体,进行信息提取
def parser_html(html):
soup = BeautifulSoup(html,'lxml')
names = soup.select('.houseList .list .plotListwrap dd p')[::3]#小区名称、小区链接、
addresses = soup.select('.houseList .list .plotListwrap dd p')[1::3] # 小区地址
years = soup.select('.houseList .list .plotListwrap dd li')[::3]#修建时间
princes = soup.select('.houseList .list .listRiconwrap .priceAverage')#小区价格
ratioes = soup.select('.houseList .list .listRiconwrap .ratio')#小区涨幅
for i in range(len(names)):
house ={
'name' : names[i].a.text.strip(),
'url': names[i].find(name='a').attrs['href'].strip(),
'address':addresses[i].text.strip(),
'prince':princes[i].text.strip()[:-4],
'ratio': ratioes[i].text.strip()
}
save_to_mongo(house) def save_to_mongo(data):
try:
if db[MONGO_TABLE].insert(data):
print('成功保存到数据库',data)
except Exception:
print('保存失败!',data) #主体函数
def main():
for page in range(1,101):
url = base_url + str(page) +'_0_0_0/'#深圳的小区房价,不同页码下的url
html = get_html(url)#完成请求,获得响应体
parser_html(html)#解析响应体,提取感兴趣的信息 if __name__=='__main__':
main()

Python 爬取 房天下的更多相关文章

  1. Python爬取房天下二手房信息

    一.相关知识 BeautifulSoup4使用 python将信息写入csv import csv with open("11.csv","w") as csv ...

  2. python爬取房天下数据Demo

    import requests from bs4 import BeautifulSoup res = requests.get('http://sh.esf.fang.com/chushou/3_3 ...

  3. python爬虫项目(scrapy-redis分布式爬取房天下租房信息)

    python爬虫scrapy项目(二) 爬取目标:房天下全国租房信息网站(起始url:http://zu.fang.com/cities.aspx) 爬取内容:城市:名字:出租方式:价格:户型:面积: ...

  4. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  5. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  6. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  7. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  8. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  9. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

随机推荐

  1. 1.pytest初尝试

    语法规范 pytest命令会默认执行以 -- test_**.py -- 或 -- **_test.py -- 命名文件的测试用例 pytest的测试用例必须以 -- test -- 开头 def t ...

  2. Redis新旧复制

    在Redis中,用户可以通过执行SALVEOF命令,让一个服务器去复制另一个服务器. 127.0.0.1:12345> SLAVEOF 127.0.0.1 6379 OK 6379的奴隶是123 ...

  3. Vue插槽slot理解与初体验 ~

    一.插槽的理解 1.官网介绍 Vue 实现了一套内容分发的 API,将 <slot> 元素作为承载分发内容的出口. 2.为什么使用插槽 Vue 中有一个重要的概念-组件,可以在开发中将子组 ...

  4. ajax 中文参数乱码问题不一定是编码格式问题。

    代码要修改用户的信息,写了三个ajax,第一个写完测试没有问题,后面俩逻辑一样的就直接复制粘贴了.到第二个ajax测试的时候发现中文会乱码 如下 $.ajax({//中文参数乱码 url: '/edi ...

  5. 11204备库升级PSU

    某系统PSU为11.2.0.4.190115,备库打补丁20年10月份(31537677)由readme可知:1.要求OPatch要求为11.2.0.3.23或之后的版本.2.非rac环境打补丁需要关 ...

  6. 数据库MHA故障分析

    一.故障分析 1.MHA故障以后是否正常:不正常 2.如果master恢复了?MHA还能自动恢复吗?:不能 3.主从恢复删除此文件 rm    saved_master_binlog_from_192 ...

  7. python日志配置及调用

    0.日志基础操作 import logging logging.basicConfig( #1.日志输出的位置,终端和文件 filename='access.log', #,不指定默认打到终端上 #2 ...

  8. Ysoserial Commons Collections3分析

    Ysoserial Commons Collections3分析 写在前面 CommonsCollections Gadget Chains CommonsCollection Version JDK ...

  9. 题解 [NOI2019]弹跳

    题目传送门 题目大意 给出 \(n\) 做城市,每座城市都有横纵坐标 \(x,y\).现在给出 \(m\) 个限制 \(p,t,l,r,d,u\),表示从 \(p\) 城市出发,可以花费 \(t\) ...

  10. iOS自动化之WDA(WebDriverAgent)安装

    1.WDA介绍 WebDriverAgent 在 iOS 端实现了一个 WebDriver server ,借助这个 server 我们可以远程控制 iOS 设备.你可以启动.杀死应用,点击.滚动视图 ...