python 爬取雪球股票代码
时间: 2023-09-28 18:04:39 浏览: 46
可以使用 Python 中的 requests 和 BeautifulSoup 库来爬取雪球股票代码。具体步骤如下:
1. 导入需要的库
```python
import requests
from bs4 import BeautifulSoup
```
2. 构造请求头和请求 URL
```python
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 = 'https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_0'
```
3. 发送请求并解析 HTML
```python
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
```
4. 提取股票代码
```python
stocks = soup.find_all('a', class_='stock__name')
codes = [stock['href'].split('/')[-1] for stock in stocks]
```
完整代码如下:
```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 = 'https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_0'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
stocks = soup.find_all('a', class_='stock__name')
codes = [stock['href'].split('/')[-1] for stock in stocks]
print(codes)
```
运行后,即可输出所有股票代码。