python爬取58同城所有租房信息_python爬虫:找房助手V1.0-爬取58同城租房信息
#!/usr/bin/python
# -*- encoding:utf-8 -*-importrequests
frombs4 importBeautifulSoup
frommultiprocessing.dummy importPool asThreadPool
importre
importdatetime
importsys
# from datetime import datetimereload(sys)
sys.setdefaultencoding('utf-8')
#得到soup,因后文通用,直接放这儿就行了defurlBS(url):
response=requests.get(url)
response.encoding = 'utf-8'soup =
BeautifulSoup(response.text,"lxml")
returnsoup
#通过交互的方式让搜索人输入想要的房屋条件,不输的话有默认值defget_source_url():
base_url='http://cd.58.com/zufang/'#首先,锁定为整租:/zufang/,然后限定为个人房源:/0/,0为个人,1为经纪人# real_url='http://cd.58.com/zufang/?isreal=true'try:
source_key=input('请按序号输入你想要的房屋来源,1为不限,2为个人房源,3为经纪人(默认为2个人房源):\n')
except:
source_key=2
source_from={1:'',2:'0/',3:'1/'} # 4:'?isreal=true',4为诚信房源专区'try:
price_min=str(input('请输入你期望的价格下限(不输默认为500):\n'))
except:
price_min='500'try:
price_max=str(input('请输入你期望的价格上限(不输默认为1000):\n'))
except:
price_max='1000'price='minprice='+price_min+'_'+price_max
try:
room_key=input('请输入你想要的房子间数:0为不限,1为1室,2为2室,3为3室,4为4室,5为4室以上(不输默认为1室):\n')
except:
room_key=1
room_num={0:'',1:'j1/',2:'j2/',3:'j3/',4:'j4/',5:'j5/'}
key_words=raw_input('请输入你想要搜索的其他关键词,如小区名称,地铁位置等(不输默认为空):\n')
source_url=base_url+source_from[source_key]+room_num[room_key]+'?'+price+'&key='+key_words
# print new_urlreturnsource_url
#
new_url='http://cd.58.com/zufang/0/j1/?minprice=600_800&PGTID=0d300008-0006-6cd9-6ba7-a7672ec996c3&ClickID=3'
#找到下一页的地址,因为58的网站很坑,它并没有显示共多少页,所以只能通过爬取他的下一页对应的href来得到下一页的链接#但是,更坑的是,他的页面进去后第一次的下一页按了后和当前页是一样的,所以我就在确定有下一页的情况下,直接用当前页+1得到下一页的urldefget_new_list(source_url):
new_url=source_url
new_url_list=[new_url]
whileTrue:
soup=urlBS(new_url)
cp=re.compile(r'/pn(.)/')
finder=soup.find('a',{'class':'next'})
iffinder:
next_url=finder['href']
now_page=cp.findall(source_url)
next_page='http://cd.58.com'+next_url
ifnow_page:
now_page=now_page[0]
newpage=str(int(now_page)+1)
new_page=cp.sub(newpage,next_page)
else:
now_page='1'newpage='2'new_page='http://cd.58.com'+next_url
new_url=new_page
else:
new_page=''break# else:
# print 'dont have next page'
# print new_urliflen(new_url_list)==1:
new_url_list.append(new_url)
elifnew_page==new_url_list[-1]:
break
else:
new_url_list.append(new_url)
new_url_listreturnnew_url_list
#得到房屋信息页的链接defget_house_url(new_url):
soup = urlBS(new_url)
href_list=soup.select('div[class="img_list"] a')
house_url_list=[]
foreach inhref_list:
href=each['href']
#print hrefhouse_url_list.append(href)
returnhouse_url_list
#爬取房屋信息,同时不要那些骗子的信息,以及一个月前更新的信息defhouse_info(house_url):
#
house_url='http://cd.58.com/zufang/26364594760504x.shtml?version=A&psid=162154127192148068945806804&entinfo=26364594760504_0'
#
print house_urlsoup=urlBS(house_url)
try:
tel=soup.find('span',{'class':'tel-num tel-num-geren pl30 f30'}).text #个人房源except:
tel=soup.find('span',{'class':'tel-num pl30 f30'}).text #中介match_tel=re.search(r'^1\d{5}.*',tel) #排除所有电话号码以0开始的人,即留固定电话的人,因为我们认为,固定房源的人是不会留固定电话的situation=soup.find('div',{'class':'description-content'}).text.strip()
# print situationmatch_si=re.search(u'(我是房东|男士勿扰|男生勿扰|限女生|微信|男士|男性|男生|女性|女的|姐妹|"+")',situation)
#更新时间update_time=soup.find('span',{'class':'pl10'}).text
update_date = datetime.datetime.strptime(update_time.replace('更新时间:',''), "%Y-%m-%d").date()
thirtyDayAgo=datetime.date.today() +
datetime.timedelta(days=-30)
day_line=(update_date -
thirtyDayAgo).days
if notmatch_tel: #认为隐藏了电话号码的,电话号码以0开始的,都是骗子,不要他# print '电话号码有问题,骗子'pass
elifmatch_si: #认为含有某些字的全部为骗子,把这些排除掉# print '内容有问题,骗子'pass
elifday_line<0: #取近30天更新的数据,时间太长了的数据没啥意义# print '已经是一个月之前的消息了'pass
else:
printhouse_url
printsituation
printtel
#标题title=soup.find('h1',{'class':'main-title
font-heiti'}).text
printtitle
#价格p=re.compile(r'\n|\t|\r| ')
rent_price=soup.find('i',{'class':'ncolor'}).text
price=p.sub('',rent_price)
printprice
#房屋大小house=soup.find_all('li',{'class':'house-primary-content-li
clearfix'})
house_content=p.sub('',house[0].text)
printhouse_content
#小区try:
house_Community=p.sub('',house[1].text)
except:
house_Community=''printhouse_Community
#位置try:
house_place=p.sub('',house[2].text)
except:
house_place=''printhouse_place
#设施try:
facility=soup.find('li',{'class':'house-primary-content-li
clearfix person-config'})
facility=p.sub('',facility.text)
except:
facility=''printfacility
#联系人contact=soup.find('li',{'class':'house-primary-content-li
clearfix person-contact'}).text
contact=p.sub('',contact)
printcontact
printupdate_time+'\n\n\n'#
a=[house_url,price,house_content,house_Community,house_place,title,situation,facility]f.write('----------------------------------------------------------------------------------\n')
f.write(house_url+'\n'+price+'\n'+house_content+'\n'+house_Community+'\n'+house_place+'\n'+title+'\n'+situation+'\n'+facility+'\n\n')
if__name__=='__main__':
source_url=get_source_url()
printsource_url
#
source_url='http://cd.58.com/zufang/0/?minprice=500_1500&key=四河'get_new_list=get_new_list(source_url)
get_new_listf=open("house_rent.txt", "w")
#先清空,然后再打开,再写入,写入时的方式是a(追加)
# f.truncate()
# f.close()
#
# f=open("house_rent.text",
"a")print'正在下载,请稍候。。。\n\n'# pool =
ThreadPool(4)fornew_url inget_new_list:
new_url=new_url.encode('utf-8').decode('utf-8')
# print new_urlhouse_url_list=get_house_url(new_url)
house_url_listforeach inhouse_url_list: #本来打算使用多线程,但是总是会报:'module' object has no attribute '_strptime'这个奇怪的错误,挣扎了许久,放弃house_info(each)
# results = pool.map(house_info,
house_url_list)
# pool.close()
# pool.join()f.close()
python爬取58同城所有租房信息_python爬虫:找房助手V1.0-爬取58同城租房信息相关推荐
- python爬取58同城租房信息_python爬虫:找房助手V1.0-爬取58同城租房信息(示例代码)...
#!/usr/bin/python # -*- encoding:utf-8 -*-importrequests frombs4 importBeautifulSoup frommultiproces ...
- python爬虫:找房助手V1.0-爬取58同城租房信息
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...
- python写爬虫4-多线程爬虫(采集58出租房信息)_python爬虫 爬取58同城上所有城市的租房信息详解...
代码如下 from fake_useragent import UserAgent from lxml import etree import requests, os import time, re ...
- python爬取控制台信息_python爬虫实战之爬取智联职位信息和博客文章信息
1.python爬取招聘信息 简单爬取智联招聘职位信息 # !/usr/bin/env python # -*-coding:utf-8-*- """ @Author ...
- python爬取豆瓣电影信息_Python爬虫入门 | 爬取豆瓣电影信息
这是一个适用于小白的Python爬虫免费教学课程,只有7节,让零基础的你初步了解爬虫,跟着课程内容能自己爬取资源.看着文章,打开电脑动手实践,平均45分钟就能学完一节,如果你愿意,今天内你就可以迈入爬 ...
- python爬取新浪微博数据中心_Python爬虫框架Scrapy实战之批量抓取招聘信息
网络爬虫抓取特定网站网页的html数据,但是一个网站有上千上万条数据,我们不可能知道网站网页的url地址,所以,要有个技巧去抓取网站的所有html页面.Scrapy是纯Python实现的爬虫框架,用户 ...
- python爬虫电影信息_Python爬虫入门 | 爬取豆瓣电影信息
这是一个适用于小白的Python爬虫免费教学课程,只有7节,让零基础的你初步了解爬虫,跟着课程内容能自己爬取资源.看着文章,打开电脑动手实践,平均45分钟就能学完一节,如果你愿意,今天内你就可以迈入爬 ...
- python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析...
Python爬虫入门实战--------一周天气预报爬取[转载][没有分析 Python爬虫入门实战--------一周天气预报爬取[转载][没有分析] 来源:https://blog.csdn.ne ...
- layui获取input信息_python爬虫—用selenium爬取京东商品信息
python爬虫--用selenium爬取京东商品信息 1.先附上效果图(我偷懒只爬了4页) 2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Hea ...
最新文章
- 第一篇:web之前端之html
- 【剑指offer】面试题03:数组中重复的数字(java)
- JS-函数(匿名-自调用-回调)-递归
- EasyUI三级联动下拉框
- 《我也能做CTO之程序员职业规划》和《.NET软件设计新思维——像搭积木一样搭建软件》新书发布会 回顾
- 【思考人生】大学(专科)三点五个学期的总结
- SQL Server之旅:(二)Could not load the DLL xpstar90.dll
- Django Celerybeat日志报错处理('NoneType' object has no attribute 'is_due')
- macOS分辨率修改器哪个好用?
- 难得干货,揭秘支付宝的2维码扫码技术优化实践之路
- Cocos Creator 虚拟摇杆
- 严选chat_拔草 | 网易严选的零食,真的“严选”了吗?
- ZigBee学习之NXP JN5169开发环境搭建
- 施一公等团队登Science封面:AI与冷冻电镜揭示「原子级」NPC结构,生命科学突破...
- IIR数字低通滤波器
- 苹果:第三方安装软件或导致严重隐私、安全风险
- python里的demo是什么意思_软件中的“DEMO” 是什么意思?游戏中的“DEMO呢?
- 用于私网的IP地址段
- 关于西门子PC Adapter USB对于PPI协议的支持问题的结论(2015-10-19相反的结论):
- 直播程序源码功能技术详解