python从巨潮网批量爬取年报
时间: 2023-10-06 07:06:36 浏览: 250
Python可以使用`requests`和`beautifulsoup`库来实现从巨潮网批量爬取年报。
首先,需要找到巨潮网年报的下载链接,可以使用浏览器的开发者工具进行查找。一般来说,年报的下载链接是以`http://www.cninfo.com.cn`开头的。
然后,可以编写Python代码来批量爬取年报。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头,模拟浏览器请求
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'}
# 巨潮网年报列表页的URL
url = 'http://www.cninfo.com.cn/new/disclosure/stock?plate=szse&stock=&searchkey=&category=category_ndbg_szsh;category_bndbg_szsh;category_yjdbg_szsh;category_sjdbg_szsh&trade=&column=szse'
# 发送GET请求获取年报列表页的HTML代码
response = requests.get(url, headers=headers)
html = response.content.decode('utf-8')
# 解析HTML代码,获取年报的下载链接
soup = BeautifulSoup(html, 'html.parser')
links = soup.find_all('a', {'href': lambda x: x and x.startswith('http://www.cninfo.com.cn/')})
# 遍历年报下载链接,分别进行下载
for link in links:
file_url = link['href']
file_name = link.text.strip()
if file_url.endswith('.pdf'):
# 发送GET请求下载年报
file_content = requests.get(file_url, headers=headers).content
with open(file_name, 'wb') as f:
f.write(file_content)
print('下载完成:', file_name)
```
需要注意的是,巨潮网有反爬虫机制,如果进行大量的爬取可能会被封IP。因此,建议使用代理IP或者降低爬取速度以避免被封。另外,爬取年报可能涉及到法律风险,请在遵守相关法律法规的前提下进行爬取。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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://img-home.csdnimg.cn/images/20241231045053.png)