Python写一串采集股票代码
时间: 2023-04-07 21:05:05 浏览: 105
以下是Python代码,可以用于采集股票代码:
```python
import requests
from bs4 import BeautifulSoup
url = "http://quote.eastmoney.com/stocklist.html"
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
stock_codes = []
for ul in soup.find_all('ul', {'class': 'stockalllist'})[0:2]:
for a in ul.find_all('a'):
href = a['href']
code = href.split('/')[-1].split('.')[0]
stock_codes.append(code)
print(stock_codes)
```
这段代码可以从东方财富网上采集股票代码,并将结果输出为一个列表。
阅读全文