
大家好,我是欧K~
所有内容仅供参考,不做他用。


接下来,按 F12 或者右键选择审查元素,搜索行政区:拱墅

这里我们可以看到所有行政区列表数据。
接下来搜索第一条信息望江 · 海潮御品公寓:

同样这里可以看到房屋信息的各项数据。
那么我们可以根据行政区来爬取整个杭州市的数据了,根据行政区爬取数据会相对多一些。
import timeimport requestsimport pandas as pd2.2 请求网页数据

defget_data(cookies,headers,filepath): screen_dict = get_region() adrs = list(screen_dict.keys()) print(adrs)for adr in adrs: print(f'当前爬取行政区:{adr} 第 1 页 ') url_1 = f'https://hz.esf.fang.com{screen_dict[adr]}i31/' print(url_1) onepageinfo,pages = get_first_page(url_1,cookies,headers,adr)if onepageinfo: insert2excel(filepath,onepageinfo)2.3 解析数据
defget_onepage(url_one,cookies,headers,adr): response = requests.get(url_one, cookies=cookies, headers=headers) soup = BeautifulSoup(response.text,'html.parser') house_list = shop_list.find_all('dl') onepageinfo = []for shop in house_list: tit_shop = shop.find_all(attrs={'class':'tit_shop'})[0].text tel_shop = shop.find_all(attrs={'class':'tel_shop'})[0].text add_shop = shop.find_all(attrs={'class':'add_shop'})[0].text label = shop.find_all(attrs={'class':'label'})[0].text price_right = shop.find_all(attrs={'class':'price_right'})[0].text2.4 获取总页数
这里需要注意,每个行政区数据量不一样,也就是页数不一样,所以需要先解析出来当前行政区有多少页数据:
soup = BeautifulSoup(response.text,'html.parser')pages = int(re.findall('共(\d+)页',soup.find_all(attrs={'class':'page_al'})[0].text)[0])2.5 保存文件
def insert2excel(filepath,allinfo):if not os.path.exists(filepath): tableTitle = ['行政区','标题','房屋信息','地址','地铁','房价'] wb = Workbook()ws = wb.activews.title = 'sheet1'ws.append(tableTitle) wb.save(filepath) time.sleep(3) wb = load_workbook(filepath)ws = wb.activews.title = 'sheet1'for info in allinfo:ws.append(info) wb.save(filepath)2.6 结果

👉 公众号后台回复【可视化项目源码】获取全部代码+数据

以上就是本期为大家整理的全部内容了,喜欢的朋友可以
点赞、点在看
也可以分享让更多人知道。
往期推荐
源码下载 | 【01-50】Pthon可视化系列文章资源(源码+数据)
可视化系统04 | 基于Python的房天下网房屋数据分析预测系统
可视化系统01 | 豆瓣电影Top250数据分析系统+数据库案例
61 | 基于Apriori算法及帕累托算法的超市销售数据分析可视化
56 | 2024年中国500强企业数据分析可视化分析可视化
53 | 基于Lasso回归和随机森林的上海链家二手房房价预测
52 | 基于KNN近邻和随机森林模型对用户转化进行分析与预测
Pandas+Pyecharts | 全国热门旅游景点数据分析可视化
可视化 | 分享一套Flask+Pyecharts可视化模板
用Python分析了3W+《独行月球》影评数据,看看观众们怎么说~


