python二手房价格预测_Python爬取赶集网北京二手房数据&R对爬取的二手房房价做线性回归分析...

前言:本文主要分为两部分:Python爬取赶集网北京二手房数据&R对爬取的二手房房价做线性回归分析。文章思路清晰,代码详细,特别适合刚刚接触Python&R的同学学习参考。

Part1:Python爬取赶集网北京二手房数据

入门爬虫一个月,所以对每一个网站都使用Xpath、BeautifulSoup、正则三种方法分别爬取,用于练习巩固。数据来源如下:

2fc7852ea30fc005e3ebea10bbbea6a1.png

本文使用BeautifulSoup讲解。

Xpath传送门:Xpath+requests爬取赶集网北京二手房数据

importrequestsimportrefromrequests.exceptionsimportRequestExceptionfrombs4importBeautifulSoupimportcsvimporttimeheaders={'User-Agent':'Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/55.0.2883.87Safari/537.36'}defget_one_page(url):try:response=requests.get(url,headers=headers)ifresponse.status_code==200:returnresponse.textreturnNoneexceptRequestException:returnNonedefparse_one_page(content):try:soup=BeautifulSoup(content,'html.parser')items=soup.find('div',class_=re.compile('js-tips-list'))fordivinitems.find_all('div',class_=re.compile('ershoufang-list')):yield{'Name':div.find('a',class_=re.compile('js-title')).text,'Type':div.find('dd',class_=re.compile('size')).contents[1].text,#tag的.contents属性可以将tag的子节点以列表的方式输出'Area':div.find('dd',class_=re.compile('size')).contents[5].text,'Towards':div.find('dd',class_=re.compile('size')).contents[9].text,'Floor':div.find('dd',class_=re.compile('size')).contents[13].text.replace('\n',''),'Decorate':div.find('dd',class_=re.compile('size')).contents[17].text,'Address':div.find('span',class_=re.compile('area')).text.strip().replace('','').replace('\n',''),'TotalPrice':div.find('span',class_=re.compile('js-price')).text+div.find('span',class_=re.co

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值