Python爬取链家地铁房数据
2017-07-21 10:21
661 查看
#coding=gbk #因为涉及到中文,utf-8会报错 ### 环境:Python 3.6### import requests import re import pandas as pd import csv from bs4 import BeautifulSoup def generate_allurl(user_in_nub): url = 'https://bj.lianjia.com/ditiefang/li647/pg{}/' for url_next in range(1, int(user_in_nub)): yield url.format(url_next) def main(): #user_in_nub = input('输入生成页数:') df = [] for i in generate_allurl(35): #总共34页 print("页码"+i) #get_allurl(i) res = requests.get(i) if res.status_code == 200: soup = BeautifulSoup(res.text, 'lxml') #获取html的文本 re_set = re.compile('<li.*?class="clear">.*?<a.*?class="img.*?".*?data-housecode="(.*?)"')#正则匹配data-house re_get = re.findall(re_set, res.text)#获取一页的二手房信息个数 print(len(re_get)) #for i in re_get: #print(i) #open_url(i) for i in range(len(re_get)): info = {} info['编号'] = re_get[i] + '号' #print(info) info['单价'] = soup.select('.unitPrice')[i].text info['地铁'] = soup.select('.subway')[i].text df.append(info) print(df) #df1 = pd.DataFrame.from_dict(info, orient='index').T #pandas_to_xlsx(df1) # 表头 # header = ['编号', '单价', '地铁'] print(len(df)) df = pd.DataFrame(df) #将结果写入csv df.to_csv('D:/dst8.csv', index=False) if __name__ == '__main__': main()
相关文章推荐
- 【Python爬虫系列】Python 爬取上海链家二手房数据
- Python爬虫——城市公交、地铁站点和线路数据采集
- [置顶] 【python 爬虫】链家天津租房在售房源数据爬虫
- Python爬取链家的数据并绘制热力图
- Python爬虫:获取链家,搜房,大众点评的数据
- 地铁译:Spark for python developers ---Spark流式数据处理
- 地铁译:Spark for python developers ---Spark处理后的数据可视化
- Python爬虫_城市公交、地铁站点和线路数据采集实例
- 地铁译:Spark for python developers ---构建Spark批处理和流处理应用前的数据准备
- python爬取链家新房数据
- 地铁译:Spark for python developers ---Spark的数据戏法
- python爬虫抓取链家租房数据
- Python数据爬虫,爬链家的二手房信息
- python爬取链家数据
- 地铁译:Spark for python developers ---Spark与数据的机器学习
- Python 数据类型、变量、运算符、注释
- python 数据采集入门
- python 实现斐波那契数列,动态删除历史数据demo,很久以前写的,分享给大家 推荐
- python处理二进制数据的方法
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱