开发
使用python和selenium打开指定页面
#coding=utf-8
from selenium import webdriver
import time
import os
br=webdriver.Firefox()
baseurl="http://quote.eastmoney.com/"
indexurl="stocklist.html"
gourl="%s%s"%(baseurl,indexurl)
br.get(gourl)
time.sleep(5)
br.quit()
获得该页全部股票链接代码
#coding=utf-8
from selenium import webdriver
import time
import os
def showlink(linklist):
for x in linklist:
print x.get_attribute('href')
print "股票数:%d"%(len(linklist))
br=webdriver.Firefox()
baseurl="http://quote.eastmoney.com/"
indexurl="stocklist.html"
gourl="%s%s"%(baseurl,indexurl)
br.get(gourl)
shxpath="/html/body/div[9]/div[2]/div/ul[1]"
szxpath="/html/body/div[9]/div[2]/div/ul[2]"
shgupiao=br.find_element_by_xpath(shxpath)
szgupiao=br.find_element_by_xpath(szxpath)
shgupiaolist=shgupiao.find_elements_by_tag_name('a')
szgupiaolist=szgupiao.find_elements_by_tag_name('a')
showlink(shgupiaolist)
br.quit()
打开一个股票链接获得数据
#coding=utf-8
from selenium import webdriver
import time
import os
br=webdriver.Firefox()
gourl="http://quote.eastmoney.com/sz300401.html"
gourlx="http://quote.eastmoney.com/sh166105.html"
gourlxx="http://quote.eastmoney.com/sh201009.html"
br.get(gourl)
hexinshujuxpath="/html/body/div[14]/div[1]/div[4]/div[1]"
#原谅我用异常处里来代替了正常该进行判断的非股票类型数据吧
try:
hexinshuju=br.find_element_by_class_name('pad5')
shujuhang=hexinshuju.find_elements_by_tag_name('tr')
print "共有数据%d行"%(len(shujuhang))
for i in range(len(shujuhang)-2):
shujulie=shujuhang[i].find_elements_by_tag_name('td')
print "%s %s\n"%(shujulie[0].text,shujulie[1].text)
print "%s\n%s"%(shujuhang[8].text,shujuhang[9].text)
except:
print "非股票数据放弃"
finally:
time.sleep(1)
br.quit()