python爬虫建筑案例可视化案例
时间: 2023-07-11 21:00:41 浏览: 253
Python实战_空气质量指数爬取(网络爬虫+数据可视化).zip
以下是一个Python爬虫建筑案例的可视化案例:
1. 爬取数据
我们可以使用Python爬虫爬取一些建筑的信息,例如高度、建造年份和位置等。这里我们以Emporis网站上的建筑信息为例:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.emporis.com/statistics/tallest-buildings'
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
buildings = []
for building in soup.find_all('div', class_='statistics__building'):
name = building.find('h4').text
height = building.find('div', class_='statistics__value').text
year = building.find_all('div', class_='statistics__value')[1].text
location = building.find('div', class_='statistics__location').text
buildings.append({'name': name, 'height': height, 'year': year, 'location': location})
```
2. 可视化数据
接下来,我们可以使用plotly库将爬取到的数据可视化出来:
```python
import plotly.graph_objs as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, specs=[[{'type': 'xy'}, {'type': 'mapbox'}]])
x = [building['name'] for building in buildings]
y = [float(building['height'].replace(',', '')) for building in buildings]
z = [building['location'] for building in buildings]
year = [int(building['year']) for building in buildings]
fig.add_trace(go.Bar(x=x, y=y), row=1, col=1)
fig.add_trace(go.Scattermapbox(lat=[40.7128], lon=[-74.0060], mode='markers', marker={'size': 10}),
row=1, col=2)
fig.update_layout(mapbox_style='open-street-map', mapbox_center_lat=40.7128, mapbox_center_lon=-74.0060,
mapbox_zoom=2)
fig.show()
```
这段代码将建筑的高度以柱状图的形式展示出来,建筑的位置以地图的形式展示出来。通过这种方式,我们可以更加直观地观察到各个建筑之间的高度差异以及它们的位置分布情况。
阅读全文