python 爬取东财公告
时间: 2023-10-10 13:02:59 浏览: 52
要使用Python爬取东财公告,可以按照以下步骤操作:
1. 导入所需的模块,包括requests、BeautifulSoup和pandas等。
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
2. 使用requests库发送GET请求,获取网页的HTML代码。
```python
url = 'http://www.eastmoney.com/guandian/dfgg.html'
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)
html = response.text
```
3. 使用BeautifulSoup解析HTML代码,提取所需的公告信息。
```python
soup = BeautifulSoup(html, 'html.parser')
announcements = soup.find_all('div', class_='news-text')
```
4. 遍历公告信息列表,提取每条公告的标题、时间和链接等信息。
```python
data = []
for announcement in announcements:
title = announcement.find('a').text
time = announcement.find('span', class_='m').text
link = announcement.find('a')['href']
data.append([title, time, link])
```
5. 将提取的信息保存到DataFrame中,方便后续处理和分析。
```python
df = pd.DataFrame(data, columns=['标题', '时间', '链接'])
```
6. 将DataFrame保存为Excel文件。
```python
df.to_excel('东财公告.xlsx', index=False)
```
经过以上步骤,就可以使用Python爬取东财公告,并将其保存为Excel文件供后续使用。