爬取胡润百富前200条数据,并进行分析,可视化,使用jupyter
时间: 2024-06-06 18:01:30 浏览: 36
好的,我会按照您的要求进行操作。
首先,我们需要爬取胡润百富榜的前200条数据。为了实现这一目标,我们可以使用Python的Requests库和BeautifulSoup库。
以下是代码实现:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.hurun.net/EN/HuList.aspx?nid=12'
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'}
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', {'class': 'List'})
rows = table.find_all('tr')
data = []
for row in rows[1:]:
rank = row.find_all('td')[0].text.strip()
name = row.find_all('td')[1].text.strip()
networth = row.find_all('td')[2].text.strip()
company = row.find_all('td')[3].text.strip()
country = row.find_all('td')[4].text.strip()
data.append([rank, name, networth, company, country])
df = pd.DataFrame(data, columns=['Rank', 'Name', 'Net Worth', 'Company', 'Country'])
df.to_csv('hurun_200.csv', index=False)
```
这段代码将从胡润网站上爬取前200位富豪的排名、姓名、财富、公司和国籍信息,并将其保存到CSV文件“hurun_200.csv”中。
接下来,我们需要进行数据分析和可视化。为了实现这一目标,我们可以使用Python的Pandas和Matplotlib库。
以下是代码实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('hurun_200.csv')
# 绘制前20位富豪的财富柱状图
top20 = df.head(20)
top20.plot(kind='bar', x='Name', y='Net Worth', legend=False)
plt.title('Top 20 Richest People in the World')
plt.xlabel('Name')
plt.ylabel('Net Worth (Billion USD)')
plt.show()
# 统计国家/地区富豪数量并绘制饼图
country_counts = df['Country'].value_counts()
country_counts.plot(kind='pie', legend=False)
plt.title('Number of Billionaires by Country/Region')
plt.show()
```
这段代码将绘制前20位富豪的财富柱状图和国家/地区富豪数量的饼图。您可以在Jupyter Notebook中执行此代码并查看结果。
希望这可以帮助您完成您的任务!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)