Python 从东方财富批量下载公司公告
时间: 2024-02-02 13:08:50 浏览: 30
以下是Python从东方财富批量下载公司公告的方法:
1.首先,需要安装requests和beautifulsoup4库。可以使用以下命令进行安装:
```shell
pip install requests
pip install beautifulsoup4
```
2.然后,使用requests库获取东方财富网站上的公司公告页面的HTML代码。可以使用以下代码:
```python
import requests
url = 'http://data.eastmoney.com/notices/industry/analysis.html'
response = requests.get(url)
html = response.text
```
3.接下来,使用beautifulsoup4库解析HTML代码,获取公司公告的链接。可以使用以下代码:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
links = soup.select('.newsitem > .title > a')
for link in links:
href = link['href']
print(href)
```
4.最后,使用requests库下载公司公告。可以使用以下代码:
```python
import os
save_dir = 'notices'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for link in links:
href = link['href']
filename = href.split('/')[-1]
filepath = os.path.join(save_dir, filename)
response = requests.get(href)
with open(filepath, 'wb') as f:
f.write(response.content)
```