python利⽤selenium爬取同花顺官⽹股票信息(⼀)俗话说⾦融市场潮起潮落,中国A股市场也是如同波浪潮汐般周⽽复始,突然觉得每次选股票太⿇烦,
于是决定写⼀个⼩程序,并加之筛选算法,最终在GUI便捷化,数据可视化,在海量的股票中选择出符合⾃⼰
决策算法的⽜股来。
本来计划⽤requests包和BeautifulSoup来写个静态的,结果发现⽹站是⽤ Ajax编写,故⽤selenium⾃动化
爬取,写下测试源代码,如下:
# coding=utf-8
import pandas as pd
import numpy as np
#import matplotlib
from selenium import webdriver
import time
if __name__=='__main__':
url='s-sec/ots911/vip_doc/18922945_0_0_1.html'
url='q.10jqka/' #同花顺官⽹涨跌幅排⾏榜
driverpath=r'C:\Program Files\Mozilla '
browser=webdriver.Firefox(executable_path=driverpath)
<(url)
print(browser.current_url)
print(browser.page_source)
with open('','w') as f:
f.write(browser.page_source)
Monday=browser.find_element_by_xpath('html/body/div[2]/div[2]')
print('=======================================')
print(Monday)
# href="javascript:void(0)" 由于页码按钮超链接是js代码,故只能selenium
Tuesday=browser.find_element_by_xpath(".//*[@id='m-page']/a[1]")
button_Tuesday=Tuesday
xpath语法 pythonbutton_Tuesday.click()
print(Tuesday)
Tuesday = browser.find_element_by_xpath(".//*[@id='m-page']/a[2]")
button_Tuesday = Tuesday
button_Tuesday.click()
print(Tuesday)
Wednesday=browser.find_element_by_xpath(".//*[@id='m-page']/a[8]") #这个⼀直是下⼀页,只有在a[2]页⾯才⾏
button_Wednesday=Wednesday
button_Wednesday.click()
time.sleep(1)
Thursday=browser.find_element_by_xpath("html/body/div[2]/div[2]") # 个股⾏情的全部
)
Friday=browser.find_element_by_xpath(".//*[@id='m-page']/span") #⾸页上⼀页 1 2 3 4 5 下⼀页尾页 ‘3/186’
)
number=()).split('/')[1] #总数
print(number)
for i in range(1,int(50),1): #这个循环的数可以⾃⼰改写,也可以⽤之前的number
Wednesday = browser.find_element_by_xpath(".//*[@id='m-page']/a[8]") # 这个⼀直是下⼀页,只有在a[2]页⾯才⾏
button_Wednesday = Wednesday
button_Wednesday.click()
time.sleep(1)
Thursday = browser.find_element_by_xpath("html/body/div[2]/div[2]") # 个股⾏情的全部
)
with open('spider_socketest.csv','a+') as f:
f.)
with open('','a+') as f:
f.)
#time.sleep(3)
print("------------end-----------------")
browser.close() #浏览器关闭
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论