今天给你们带来了通过selenium获取东方财富股票数据实例,用于学习。
1.基本完整实例:
from selenium import webdriver
from bs4 import BeautifulSoup
# 创建Chrome浏览器驱动对象
driver = webdriver.Chrome()
# 构建URL
url = "http://quote.eastmoney.com/center/gridlist.html#hs_a_board"
# 发起GET请求
driver.get(url)
# 等待页面加载完成
driver.implicitly_wait(10)
# 解析响应内容
soup = BeautifulSoup(driver.page_source, "html.parser")
# 获取股票信息表格
table = soup.select(".table_wrapper-table tbody")
# 遍历每行数据
for row in table[0].find_all("tr"):
# 获取每列数据
columns = row.find_all("td")
# 提取所需的股票信息
stock_name = columns[1].text
h_share_code = columns[2].text
hkd_price = columns[3].text
change_percent = columns[4].text
hk_stock_bar = columns[5].text
a_share_code = columns[6].text
rmb_price = columns[7].text
a_change_percent = columns[8].text
a_stock_bar = columns[9].text
price_ratio = columns[10].text
premium_percent = columns[11].text
# 打印股票信息
print("股票名称:", stock_name)
print("H股代码:", h_share_code)
print("最新价(HKD):", hkd_price)
print("涨跌幅:", change_percent)
print("港股吧:", hk_stock_bar)
print("A股代码:", a_share_code)
print("最新价(RMB):", rmb_price)
print("涨跌幅(A股):", a_change_percent)
print("A股吧:", a_stock_bar)
print("比价(A/H):", price_ratio)
print("溢价(A/H)%:", premium_percent)
print("---------------------------------------")
# 关闭浏览器
driver.quit()
输出:
---------------------------------------
股票名称: 300538
H股代码: 同益股份
最新价(HKD): 股吧 资金流 数据
涨跌幅: 23.01
港股吧: 12.79%
A股代码: 2.61
最新价(RMB): 42.09万
涨跌幅(A股): 9.55亿
A股吧: 25.34%
比价(A/H): 24.48
溢价(A/H)%: 19.31
---------------------------------------
2.获取股票信息,跳页,保存到csv里:
import csv
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
# 创建Chrome浏览器驱动选项对象
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# 创建Chrome浏览器驱动对象
driver = webdriver.Chrome(options=chrome_options)
# 构建URL
url = "http://quote.eastmoney.com/center/gridlist.html#hs_a_board"
# 发起GET请求
driver.get(url)
# 等待页面加载完成
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".table_wrapper-table")))
# 创建CSV文件
csv_file = open("股票数据.csv", "w", newline="", encoding="utf-8-sig")
writer = csv.writer(csv_file)
# 写入表头信息
writer.writerow([
"股票名称",
"沪股通代码",
"港币价格",
"涨跌幅",
"港股通卖出价",
"A股代码",
"人民币价格",
"涨跌幅(A股)",
"A股卖出价",
"价格比",
"溢价率"
])
while True:
# 解析响应内容
soup = BeautifulSoup(driver.page_source, "html.parser")
# 获取股票信息表格
table = soup.select(".table_wrapper-table tbody")
# 遍历每行数据
for row in table[0].find_all("tr"):
# 获取每列数据
columns = row.find_all("td")
# 提取所需的股票信息
stock_name = columns[1].text
h_share_code = columns[2].text
hkd_price = columns[3].text
change_percent = columns[4].text
hk_stock_bar = columns[5].text
a_share_code = columns[6].text
rmb_price = columns[7].text
a_change_percent = columns[8].text
a_stock_bar = columns[9].text
price_ratio = columns[10].text
premium_percent = columns[11].text
# 写入CSV文件
writer.writerow([
stock_name,
h_share_code,
hkd_price,
change_percent,
hk_stock_bar,
a_share_code,
rmb_price,
a_change_percent,
a_stock_bar,
price_ratio,
premium_percent
])
# 查找下一页按钮
next_button = driver.find_element(By.CSS_SELECTOR, ".paginate_button.next")
# 判断下一页按钮是否可点击,如果不可点击,则表示已经是最后一页
if "disabled" in next_button.get_attribute("class"):
break
# 点击下一页按钮
next_button.click()
# 等待页面加载完成
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".table_wrapper-table")))
# 关闭CSV文件
csv_file.close()
# 关闭浏览器
driver.quit()
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/11712/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取