Python上海酒店爬虫数据可视化分析和推荐查询系统
warning:
这篇文章距离上次修改已过187天,其中的内容可能已经有所变动。
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# 爬取数据
def crawl_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find_all('div', class_='price')
prices = [int(item.text.strip()[1:]) for item in data]
return prices
# 数据可视化
def visualize_data(data):
plt.hist(data, bins=50)
plt.title('酒店价格分布')
plt.xlabel('价格(人民币)')
plt.ylabel('酒店数量')
plt.show()
# 数据分析和报告
def analyze_data(data):
mean_price = pd.Series(data).mean()
median_price = pd.Series(data).median()
mode_price = pd.Series(data).mode()[0]
print(f'平均价格: {mean_price}')
print(f'中位数价格: {median_price}')
print(f'最常见价格: {mode_price}')
# 主函数
def main():
url = 'https://www.example.com/hotels'
prices = crawl_data(url)
analyze_data(prices)
visualize_data(prices)
if __name__ == '__main__':
main()
这个代码实例展示了如何使用Python爬取数据、进行简单的数据分析、可视化分析结果,并且提供了一个模拟的网络爬虫URL。实际应用中,你需要替换为正确的URL和相应的数据解析方法。
评论已关闭