安居客无锡二手房

  • 用selenium模拟浏览器抓取
  • 使用requests抓取网页,用bs4解析网页
  • 使用lxml解析网页 (xpath)

网址: https://wuxi.anjuke.com/sale/

用selenium模拟浏览器抓取

# 用selenium模拟浏览器抓取
# selenium版本3.141.0
# firefox版本86.0
# geckodriver0.29.0
# geckodriver下载地址:https://github.com/mozilla/geckodriver/releases
from selenium import webdriver
import time
import pandas as pd
driver = webdriver.Firefox(executable_path = r'D:\geckodriver.exe') #geckodriver.exe的存放位置
link = "https://wuxi.anjuke.com/sale/"
driver.get(link)
df = pd.DataFrame(columns=('title', 'name', 'price', 'price_area', 'no_room', 'area', 'orientations', 'floor', 'year', 'address', 'tags'))
a = 0
for x in range(50):print('正在爬取第%s页'%(x+1))house_list = driver.find_elements_by_css_selector('div.property')for i in range(len(house_list)):title = house_list[i].find_elements_by_css_selector('h3.property-content-title-name')[0].textname = house_list[i].find_elements_by_css_selector('p.property-content-info-comm-name')[0].textprice = house_list[i].find_elements_by_css_selector('span.property-price-total-num')[0].textprice_area = house_list[i].find_elements_by_css_selector('p.property-price-average')[0].textno_rooms = house_list[i].find_elements_by_xpath('//div[@class="property"]/a/div[2]/div[1]/section/div[1]/p[1]/span')no_room = no_rooms[6*i].text+no_rooms[6*i+1].text+no_rooms[6*i+2].text+no_rooms[6*i+3].text+no_rooms[6*i+4].text+no_rooms[6*i+5].textinfos = house_list[i].find_elements_by_xpath('//div[@class="property-content-info"]/p[@class="property-content-info-text"]')area = infos[4*i].textorientations = infos[4*i+1].textfloor = infos[4*i+2].texttry:year = infos[4*i+3].textexcept:year = ''address = house_list[i].find_elements_by_css_selector('p.property-content-info-comm-address')[0].text[:2]tag_list = house_list[i].find_elements_by_xpath('//div[@class="property"]['+str(i)+'+1]//div[@class="property-content-info"]/span')tags = [i.text for i in tag_list]row = {'title':title, 'name':name, 'price':price, 'price_area':price_area, 'no_room':no_room, 'area':area, 'orientations':orientations, 'floor':floor, 'year':year, 'address':address, 'tags':tags}df.loc[a]= row #将数据存储至df第a+1行a += 1time.sleep(5) #间隔5秒再爬取下一个页面next_page = driver.find_elements_by_css_selector('a.next')[0] #点击下一页next_page.click()
print('爬取完毕,一共爬取%s条数据'%(a))

使用requests抓取网页,用bs4解析网页

# 使用requests抓取网页,用bs4解析网页
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
from functools import reduce
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
df = pd.DataFrame(columns=('title', 'name', 'price', 'price_area', 'no_room', 'area', 'orientations', 'floor', 'year', 'address', 'tags', 'broker', 'rate', 'company'))
a = 0
for i in range(1,51):print('正在爬取第%s页'%(i))link = 'https://wuxi.anjuke.com/sale/p'+str(i) #由于网站初次访问需要验证码,运行程序前需要先手动打开一次网页输入验证码r = requests.get(link, headers = headers)soup = BeautifulSoup(r.text, 'lxml')house_list = soup.find_all('div', class_="property")for house in house_list:title = house.find('div', class_ ='property-content-title').h3.text.strip()price = house.find('span', class_ ='property-price-total-num').text.strip()price_area = house.find('p', class_='property-price-average').text.strip()no_room = house.find('div', class_='property-content-info').contents[0].text.strip()area = house.find('div', class_='property-content-info').contents[2].text.strip()orientations = house.find('div', class_='property-content-info').contents[4].text.strip()floor = house.find('div', class_='property-content-info').contents[6].text.strip()try:year = house.find('div', class_='property-content-info').contents[8].text.strip()except:year = ''name = house.find('p', class_='property-content-info-comm-name').text.strip()address = house.find('p', class_='property-content-info-comm-address').text.strip()[:2]tag_list = house.find_all('span', class_='property-content-info-tag')tags = [i.text for i in tag_list]broker = house.find('span', class_='property-extra-text').textbroker_list = house.find_all('span', class_='property-extra-text')rate = broker_list[1].text.strip()[:3]try:company = broker_list[2].text.strip()except:company = ''row = {'title':title, 'name':name, 'price':price, 'price_area':price_area,'no_room':no_room, 'area':area, 'orientations':orientations,'floor':floor, 'year':year, 'address':address, 'tags':tags,'broker':broker, 'rate':rate, 'company':company}df.loc[a]= rowa += 1time.sleep(5)
print('爬取完毕,一共爬取%s条数据'%(a))

使用lxml解析网页 (xpath)

# 使用lxml解析网页 (xpath)
import requests
from lxml import etree
import pandas as pd
import time
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
df = pd.DataFrame(columns=('title', 'name', 'price', 'price_area', 'no_room', 'area', 'orientations', 'floor', 'year', 'address', 'tags', 'broker', 'rate', 'company'))
a = 0
for x in range(50):print('正在爬取第%s页'%(x+1))time.sleep(5)link = "https://wuxi.anjuke.com/sale/p"+str(x+1)headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'} r = requests.get(link, headers= headers)html = etree.HTML(r.text)title_list = html.xpath('//h3[@class="property-content-title-name"]/text()')name = html.xpath('//p[@class="property-content-info-comm-name"]/text()')price = html.xpath('//span[@class="property-price-total-num"]/text()')price_area = html.xpath('//p[@class="property-price-average"]/text()')no_rooms = html.xpath('//div[@class="property"]/a/div[2]/div[1]/section/div[1]/p[1]/span/text()')infos = html.xpath('//div[@class="property-content-info"]/p[@class="property-content-info-text"]/text()')info = [i.strip() for i in infos]address = html.xpath('//p[@class="property-content-info-comm-address"]/span[1]/text()')broker = html.xpath('//span[@class="property-extra-text"][1]/text()')rate = html.xpath('//span[@class="property-extra-text"][2]/text()')company = html.xpath('//span[@class="property-extra-text"][3]/text()')for i in range(len(title_list)):tags = html.xpath('//div[@class="property"]['+str(i)+'+1]//div[@class="property-content-info"]/span/text()')no_room = no_rooms[6*i]+no_rooms[6*i+1]+no_rooms[6*i+2]+no_rooms[6*i+3]+no_rooms[6*i+4]+no_rooms[6*i+5]row = {'title':title_list[i], 'name':name[i], 'price':price[i], 'price_area':price_area[i],'no_room':no_room, 'area':info[4*i],'orientations':info[4*i+1],'floor':info[4*i+2], 'year':info[4*i+3],'address':address[i], 'tags':tags, 'broker':broker[i],'rate':rate[i], 'company':company[i]}df.loc[a]=rowa+=1

安居客无锡二手房数据获取相关推荐

  1. Python3 爬虫实战 — 安居客武汉二手房【requests、Beautiful Soup、CSV】

    爬取时间:2019-10-09 爬取难度:★★☆☆☆☆ 请求链接:https://wuhan.anjuke.com/sale/ 爬取目标:爬取武汉二手房每一条售房信息,包含地理位置.价格.面积等,保存 ...

  2. scrapy爬虫实战:安居客深圳二手房

    温馨提示:想要本次爬虫源代码的同学 请关注公众号:python小咖 回复 ' 安居客爬虫 ' 获取源码 --------------------------------- 接下来进入正题 本次爬虫实现 ...

  3. 安居客广州二手房定价分析

    数据 数据爬虫所得,为安居客广州二手房信息,由于数据量不大,所有分析只是针对这个二手房网站上发布的二手房信息所进行的一些简单分析,不能避免偶然性.本人对房价分析亦没什么了解,以下均只是当作数据分析的练 ...

  4. 安居客python二手房数据预处理

    题记: 继上篇对安居客二手房数据进行爬取之后,接下来是对安居客进行数据预处理. 一: 观察上述数据可以发现爬取的数据比较杂乱无章,这时需要我们对数据进行处理,比如把一些无效数据清除掉,还有一些机器无法 ...

  5. 实战项目一、安居客(北京) 二手房抓取房源信息

    一.首先明确爬取的数据为安居客(北京)的二手房源的数据信息,主要有房源链接地址,房源价格,房源单价,房源规模,房源大小,房源建造年份,房源地址. https://beijing.anjuke.com/ ...

  6. python3 beautiful爬取安居客的二手房信息

    今天有一位同学找我爬取安居客的信息,安居客网站有反爬虫机制,所以不能简单用requests来做,这里用selenium来模拟获取url网页. 环境 mac, python3.7 beautifulso ...

  7. 菜鸟爬虫——获取安居客二手房信息

    以安居客二手房为例 前言 了解爬虫 爬虫目录结构 爬虫主体代码 items.py 反反爬虫策略 运行爬虫 前言 因为需要一些二手房数据,菜鸟开启了爬虫之路!不过需要注意的是,在爬取数据时,要遵守< ...

  8. bs4+phantomjs爬取安居客二手房信息

    bs4+phantomjs爬取安居客二手房信息 这是我的第一篇博客,希望通过养成写博客的习惯来督促自己学习. 开发环境以及需要安装的模块 - Python3.6 - requests pip inst ...

  9. 爬虫系列(1):极简爬虫——基于requests和re爬取安居客上海二手房价数据

    爬虫系列(1):极简爬虫--基于requests和re爬取安居客上海二手房价数据 入坑爬虫已经有一年多,一直想好好记录下从各位前辈和大佬处学到的技术,因此开了一个爬虫系列,想借此细致地介绍和演示其中的 ...

最新文章

  1. 【第44题】【062题库】2019年OCP认证062考试新题
  2. 网络编程--connect()、listen()、accept()
  3. python 福利彩票_使用Python买福彩,5个数字,20选5,有没买过
  4. 部署不能产生class文件的问题
  5. python使用HDF文件格式,保存多个类型的数据到一个文件
  6. 52 - 算法 - LeetCode 28 - 实现 strStr() -kmp
  7. 苹果手机升级13无法开机_苹果手机更新系统后无法开机
  8. 基于OpenCV的银行卡号识别系统实现(一)----- 银行卡号识别步骤
  9. python批量查询高德地图经纬度(支持xlxs)
  10. ApacheCN 翻译/校对/笔记整理活动进度公告 2019.9.13
  11. STM32F1主从定时器设置
  12. python菜单怎么做_Python 城市菜单详解(超详解)
  13. layui 读取本地excel内容_layui-excel
  14. 合同比对系统,告别人工比对,差异比对率100
  15. STC12C5A60S2自主适应时钟延时函数
  16. 每日学到 20 - 封装、访问修饰符
  17. Linux使用scp命令远程复制/上传文件
  18. 职场丨女生:啊啊啊,明天面试穿什么?【带图】
  19. 易中天品汉代风云人物09:刘邦崛起之谜
  20. vue实战-产品详情页(轮播图、放大镜)

热门文章

  1. 5G千兆工业路由器 poe供电
  2. 计算机人机交互接口论文,人机交互小论文
  3. 恒丰银行计算机岗位面试题,2019恒丰银行招聘面试试题及参考答案
  4. janusgraph源码分析1-下载编译启动 1
  5. 移动测试基础 Android 应用测试总结
  6. 云画册php,神策指标设计及埋点方案介绍
  7. Acala TC4 糖果节历程总结
  8. 【游戏算法】2D游戏中聚光灯效果
  9. 数码宝贝服务器连接中断,数码宝贝新世纪服务器满了 问题解决方法分享
  10. MongoDB不同压缩算法的影响