怎么用python爬取同花顺数据
时间: 2023-10-04 22:13:21 浏览: 105
你可以使用 Python 中的 requests 库和 BeautifulSoup 库来爬取同花顺数据。以下是一个简单的示例代码,用于爬取同花顺网站上的股票数据:
```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://q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/1/ajax/1/'
# 发送请求
response = requests.get(url, headers=headers)
# 解析html
soup = BeautifulSoup(response.text, 'html.parser')
# 提取数据
table = soup.find('table', {'class': 'm-table J-ajax-table'})
tbody = table.find('tbody')
for tr in tbody.find_all('tr'):
tds = tr.find_all('td')
stock_name = tds[1].text.strip()
stock_price = tds[3].text.strip()
stock_change = tds[5].text.strip()
print(stock_name, stock_price, stock_change)
```
注意,爬取网站数据需要注意法律法规和网站规定,务必遵守相关规定。