import pandas as pd import requests from bs4 import BeautifulSoup #数据爬取 header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'} # 步骤4 数据爬取:解析租房房产信息 def getdetail(url, info=[]): try: response = requests.get(url, headers=header) html_doc = response.text soup = BeautifulSoup(html_doc, 'html.parser') alldetail = soup.findAll('div', attrs={'content__list--item'}) for details in alldetail: details = details.get_text() details = details.replace(' ', '').split('\n') while "" in details: details.remove("") while "/" in details: details.remove("/") info.append(details) return info except Exception as e: print(e) return getdetail(url) info = [] for page in range(1, 21): url = 'https://gz.lianjia.com/zufang/zengcheng/pg' + str(page) print("\r开始抓取数据", end='') info += getdetail(url) #保存数据 def getkey(info, key=''): ls = [] for i in info: if key in i: ls.append(key) else: ls.append('') return ls def getdata(info): name = [i[0] for i in info] address = [i[1] for i in info] area = [i[2] for i in info] oriented = [i[3] for i in info] type = [i[4] for i in info] floor = [i[5] for i in info] price = [i[-1] for i in info] traffic = getkey(info, '近地铁') hardcover = getkey(info, '精装') paymonth = getkey(info, '月租') return pd.DataFrame({ 'name': name, 'address': address, 'area': area, 'oriented': oriented, 'type': type, 'floor': floor, 'price': price, 'traffic': traffic, 'hardcover': hardcover, 'paymonth': paymonth }) data = getdata(info) data.to_csv('data.csv',index=False)
时间: 2024-02-14 08:17:48 浏览: 154
python数据分析与可视化 import pandas as pd import numpy as np import m
这段代码是用 Python 爬取广州链家网站租房信息,并且保存到本地 data.csv 文件中。其中使用 requests 库和 BeautifulSoup 库来解析网页内容,获取租房信息。爬取的信息包括房源名称、地址、面积、朝向、户型、楼层、价格、是否近地铁、是否精装、月租等信息。最后使用 pandas 库将获取到的信息保存到本地 CSV 文件中。
阅读全文