python雪球网沪深港美股情爬取
时间: 2023-12-10 07:35:48 浏览: 234
以下是使用Python爬虫实现雪球网沪深港美股情爬取的方法:
1. 首先,我们需要安装requests和beautifulsoup4库。可以使用以下命令进行安装:
```shell
pip install requests
pip install beautifulsoup4
```
2. 导入所需库:
```python
import requests
from bs4 import BeautifulSoup
```
3. 构造请求头和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'
```
4. 发送请求并解析HTML:
```python
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
```
5. 提取所需数据:
```python
# 沪深股市
hs_stocks = soup.select('#table_wrapper-table > div > table > tbody > tr')
for stock in hs_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
# 港股
hk_stocks = soup.select('#table_wrapper-table-HK > div > table > tbody > tr')
for stock in hk_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
# 美股
us_stocks = soup.select('#table_wrapper-table-US > div > table > tbody > tr')
for stock in us_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
```
6. 完整代码:
```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')
# 沪深股市
hs_stocks = soup.select('#table_wrapper-table > div > table > tbody > tr')
for stock in hs_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
# 港股
hk_stocks = soup.select('#table_wrapper-table-HK > div > table > tbody > tr')
for stock in hk_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
# 美股
us_stocks = soup.select('#table_wrapper-table-US > div > table > tbody > tr')
for stock in us_stocks:
name = stock.select('td')[0].text
price = stock.select('td')[1].text
print(name, price)
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![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)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)