python爬取链家并制成柱状图
时间: 2023-12-12 22:57:01 浏览: 29
抱歉,作为AI语言模型,我无法演示和展示图形。但我可以提供一些Python爬取链家并制成柱状图的思路和代码片段:
1. 使用requests和BeautifulSoup库进行网页爬取和解析:
```
import requests
from bs4 import BeautifulSoup
url = "https://bj.lianjia.com/chengjiao/"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
```
2. 从网页中提取需要的房价信息:
```
prices = []
for price in soup.find_all('div', {'class': 'totalPrice'}):
prices.append(float(price.span.string))
```
3. 使用matplotlib库制成柱状图:
```
import matplotlib.pyplot as plt
plt.hist(prices, bins=50)
plt.xlabel('Price')
plt.ylabel('Number of houses')
plt.show()
```
以上是一个简单的示例,您可以根据自己的需要和兴趣进行更多的数据处理和图形展示。