Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从stocktwits下载数据_Python_Stocktwits - Fatal编程技术网

Python 从stocktwits下载数据

Python 从stocktwits下载数据,python,stocktwits,Python,Stocktwits,我的目标是通过python从stocktwits下载推文。可悲的是,我是一个完全的python新手,而且只停留在最基本的方面 我找到了Jason Haury写的剧本。遗憾的是,我没能让它运行起来。到目前为止,我所做的是下载“api.py”和“requestors.py”脚本,并用我的访问令牌替换后一个“ST_ACCESS_TOKEN”。但是,当我运行命令“get_watched_stocks(my_watchlist_id)”时,出现以下错误: “ipython-input-12-b889976

我的目标是通过python从stocktwits下载推文。可悲的是,我是一个完全的python新手,而且只停留在最基本的方面

我找到了Jason Haury写的剧本。遗憾的是,我没能让它运行起来。到目前为止,我所做的是下载“api.py”和“requestors.py”脚本,并用我的访问令牌替换后一个“ST_ACCESS_TOKEN”。但是,当我运行命令“get_watched_stocks(my_watchlist_id)”时,出现以下错误:

“ipython-input-12-b889976b3838>进入关注股票(wl_id)

-->117 wl=R.get_json(ST_BASE_URL+'watchlists/show/{}.json'。格式(wl_id),参数=ST_BASE_参数)

TypeError:必须使用请求实例作为第一个参数调用未绑定方法get_json()(改为使用get str instance)”

有人知道我可能做错了什么吗? 如果没有:请有人一步一步地解释一下我如何使用Haury先生或其他脚本从stocktwits下载推文


提前谢谢

如果查看在requestors.py中创建的Requests类,看起来创建者希望它们是类上的静态方法,但忘记实际将它们设置为静态方法。如果您进入该文件并在两个函数定义上方放置一个
@staticmethod
,它将起作用。比如说


def get_json(url,参数=None):

现在变成


@静力学方法
def get_json(url,参数=None):


测试并确认

您也可以使用selenium,我的脚本用于抄送抄本,但您可以将其应用于stocktwits的任何帐户:

###########################################################################
###  This script is a web scraper for stocktwits.                       ###
##   applied specifically on cc_transcripts   .                         ###
###  To use it you need first to install Python 3.5.2 on your computer. ###
###  Install the module "Selenium" 3.1.1, and "chromedriver.exe"        ###
###########################################################################


from selenium import webdriver
import sys
import time
from selenium.webdriver.common.keys import Keys

#only for Chrome, for firefox need another driver
print("Loading... Please wait")
Pathwebdriver="D:\\Programs\\Python\\Python35-32\\Scripts\\chromedriver.exe"
driver = webdriver.Chrome(Pathwebdriver)
#website to analyse
driver.get("https://stocktwits.com/cctranscripts?q=cctranscripts")


#Scrolling of the webpage
ScrollNumber=3
print(str(ScrollNumber)+ " scrolldown will be done.")
for i in range(1,ScrollNumber):  #scroll down X times
    print("Scrolling... #"+str(i))
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(2) #Delay between 2 scrolls down to be sure the page loaded, 1s is too short some loading take longer


#retrieving source code     
html_source = driver.page_source
data = str(html_source.encode('utf-8'))
#driver.close()#close of chrome to able the opwning of new windows and to save source code.

#Saving source code (in the same folder as this script)
SaveSource = False
if SaveSource:
    text_file = open("SourceCode.html", "w")
    text_file.write(data)
    text_file.close()


#Analysis of the source code
PosScanning=1
GlobalList=[]
print("Processing data")
while data[PosScanning:].find("picked")>0:

    PosPick=data[PosScanning:].find("picked") +PosScanning
    List = [0, 0, 0, 0, 0, 0, 0] #Ticker,Nb of shares, Text of stocktwits,Link,Price of buying, Date, Text of CC_transcript

    #Quote
    dataBis=data[PosPick::-1] #reading the string backward
    PosBegin=PosPick - dataBis.find(">") +1 #looking for the begining of the text
    data=data[PosBegin:] #shortening the string each loop to increase the speed of processing
    PosEnd=data.find("<")#looking for the end of the text
    #print(data[PosBegin:PosEnd])
    List[2]=data[:PosEnd].replace(","," ")

    #Nb of shares
    List[1]=List[2].split(' up', 1 )[1]
    List[1]=List[1].split('share', 1 )[0]
    List[1]=List[1].replace(" ","")

    #link to the transcript
    PosLinkBegin=data.find("href=")+6
    PosLinkend=data.find("\"",PosLinkBegin,PosLinkBegin+3000)
    #print(data[PosLinkBegin:PosLinkend])
    List[3]=data[PosLinkBegin:PosLinkend]

    #Symbol
    PosSymbolBegin=data.find("data-symbol=")+13
    PosSymbolEnd=data.find("\"",PosSymbolBegin,PosSymbolBegin+300)
    #print(data[PosSymbolBegin:PosSymbolEnd])
    List[0]=data[PosSymbolBegin:PosSymbolEnd]

    #data-body, the "picked" is repeat 2 times, need to ignore it
    PosBody1=data.find("picked",PosSymbolEnd,PosSymbolEnd+10000)+100
    PosBody2=data.find("picked",PosBody1,PosBody1+10000)

    PosScanning=PosBody2 +100   
    GlobalList.append(List) 



#Opening Link to retrieve information
print("Opning links to retrieve detailed information form CC_transcript")
j=1
for item in GlobalList:
    print("Retrieving data: " +str(j)+"/"+str(len(GlobalList)))
    driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')#open tab
    driver.get(item[3]) 
    html_source2 = driver.page_source
    data2 = str(html_source2.encode('utf-8'))
    #text of CC_transcript
    TextePos=data2.find("$(\"#meaning\").popover();")
    item[6] = data2[TextePos+40:TextePos+1000].replace(","," ")  
    #price of Shares
    BuyPos=item[6].find("place at")+10
    BuyPosend=item[6][BuyPos:].find("share")+BuyPos +6
    item[4]=item[6][BuyPos:BuyPosend]  
    #date
    DatePos=item[6].find(" on ")
    DatePosEnd=item[6][DatePos:].find(".")+DatePos
    item[5]=item[6][DatePos+4:DatePosEnd] 
    j=j+1
driver.close()  

#output of final data   
print("Writting data in .csv file")
f = open('stocktwits.csv','w')
f.write("Ticker")
f.write(' , ')
f.write("Nb of shares")
f.write(' , ')
f.write("Text of stocktwits")
f.write(' , ')
f.write("Link")
f.write(' , ')
f.write("Price of buying")
f.write(' , ')
f.write("Date")
f.write(' , ')
f.write("Text of CC_transcript")
f.write('\n')
for item in GlobalList:
    for elem in item:
        f.write(elem)
        f.write(' , ')# excel change of column
    f.write('\n')   # excel change of line
f.close()

time.sleep(5)   
print("Done")
###########################################################################
###此脚本是stocktwits的web刮板###
##特别适用于cc_成绩单###
###要使用它,首先需要在计算机上安装Python 3.5.2###
###安装模块“Selenium”3.1.1和“chromedriver.exe”###
###########################################################################
从selenium导入webdriver
导入系统
导入时间
从selenium.webdriver.common.keys导入密钥
#仅适用于Chrome,firefox需要另一个驱动程序
打印(“正在加载…请稍候”)
Pathwebdriver=“D:\\Programs\\Python\\Python35-32\\Scripts\\chromedriver.exe”
driver=webdriver.Chrome(路径webdriver)
#分析网站
驱动程序。获取(“https://stocktwits.com/cctranscripts?q=cctranscripts")
#滚动网页
滚动编号=3
打印(str(ScrollNumber)+“将完成向下滚动”)
对于范围内的i(1,滚动编号):#向下滚动X次
打印(“滚动…#“+str(i))
执行脚本(“window.scrollTo(0,document.body.scrollHeight);”)
时间。睡眠(2)#在两次向下滚动之间延迟,以确保页面已加载,1s太短一些加载需要更长时间
#检索源代码
html\u source=driver.page\u source
data=str(html\u source.encode('utf-8'))
#driver.close()#关闭chrome以允许选择新窗口并保存源代码。
#保存源代码(与此脚本位于同一文件夹中)
SaveSource=False
如果保存源:
text_file=open(“SourceCode.html”、“w”)
text_file.write(数据)
text_file.close()
#源代码分析
posscaning=1
全球列表=[]
打印(“处理数据”)
数据[posscaning:]时。查找(“拾取”)>0:
PosPick=data[PosScanning:]查找(“拾取的”)+PosScanning
列表=[0,0,0,0,0,0]#股票代码、股票编号、股票文本、链接、买入价格、日期、抄送抄本文本
#引述
dataBis=data[PosPick::-1]#向后读取字符串
PosBegin=PosPick-dataBis.find(“>”)+1#查找文本的开头
data=data[PosBegin:]#缩短每个循环的字符串以提高处理速度
PosEnd=data.find(“@annach看看,StockTwits的REST-API的Python包装器

由于该项目非常年轻,还远远不够完美,但要在观察列表上获得符号列表,您只需执行以下操作:

pip安装pytwits

然后:

import pytwits


def main():

    access_token = 'TOKEN'
    stocktwits = pytwits.StockTwits(access_token=access_token)

    watchlist = stocktwits.watchlists(path='show', id='WL_ID_HERE')
    print('\n\n'.join([symbol['symbol'] for symbol in watchlist.symbols]))


if __name__ == '__main__':
    main()

摘自。

嘿,埃里克,谢谢你的回答!我将“api.py”中的代码改为“wl=R().get_json(ST_BASE_URL+”watchlists/show/{}.json'.format(wl_id),params=ST_BASE_params)”,但现在我收到以下错误消息:TypeError:“get_json()为关键字参数'params'获取了多个值。”“我真的不知道如何处理这个问题:/我已经更新了答案,以反映对代码的一个更简单的更改,从而使其按设计工作。
###########################################################################
###  This script is a web scraper for stocktwits.                       ###
##   applied specifically on cc_transcripts   .                         ###
###  To use it you need first to install Python 3.5.2 on your computer. ###
###  Install the module "Selenium" 3.1.1, and "chromedriver.exe"        ###
###########################################################################


from selenium import webdriver
import sys
import time
from selenium.webdriver.common.keys import Keys

#only for Chrome, for firefox need another driver
print("Loading... Please wait")
Pathwebdriver="D:\\Programs\\Python\\Python35-32\\Scripts\\chromedriver.exe"
driver = webdriver.Chrome(Pathwebdriver)
#website to analyse
driver.get("https://stocktwits.com/cctranscripts?q=cctranscripts")


#Scrolling of the webpage
ScrollNumber=3
print(str(ScrollNumber)+ " scrolldown will be done.")
for i in range(1,ScrollNumber):  #scroll down X times
    print("Scrolling... #"+str(i))
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(2) #Delay between 2 scrolls down to be sure the page loaded, 1s is too short some loading take longer


#retrieving source code     
html_source = driver.page_source
data = str(html_source.encode('utf-8'))
#driver.close()#close of chrome to able the opwning of new windows and to save source code.

#Saving source code (in the same folder as this script)
SaveSource = False
if SaveSource:
    text_file = open("SourceCode.html", "w")
    text_file.write(data)
    text_file.close()


#Analysis of the source code
PosScanning=1
GlobalList=[]
print("Processing data")
while data[PosScanning:].find("picked")>0:

    PosPick=data[PosScanning:].find("picked") +PosScanning
    List = [0, 0, 0, 0, 0, 0, 0] #Ticker,Nb of shares, Text of stocktwits,Link,Price of buying, Date, Text of CC_transcript

    #Quote
    dataBis=data[PosPick::-1] #reading the string backward
    PosBegin=PosPick - dataBis.find(">") +1 #looking for the begining of the text
    data=data[PosBegin:] #shortening the string each loop to increase the speed of processing
    PosEnd=data.find("<")#looking for the end of the text
    #print(data[PosBegin:PosEnd])
    List[2]=data[:PosEnd].replace(","," ")

    #Nb of shares
    List[1]=List[2].split(' up', 1 )[1]
    List[1]=List[1].split('share', 1 )[0]
    List[1]=List[1].replace(" ","")

    #link to the transcript
    PosLinkBegin=data.find("href=")+6
    PosLinkend=data.find("\"",PosLinkBegin,PosLinkBegin+3000)
    #print(data[PosLinkBegin:PosLinkend])
    List[3]=data[PosLinkBegin:PosLinkend]

    #Symbol
    PosSymbolBegin=data.find("data-symbol=")+13
    PosSymbolEnd=data.find("\"",PosSymbolBegin,PosSymbolBegin+300)
    #print(data[PosSymbolBegin:PosSymbolEnd])
    List[0]=data[PosSymbolBegin:PosSymbolEnd]

    #data-body, the "picked" is repeat 2 times, need to ignore it
    PosBody1=data.find("picked",PosSymbolEnd,PosSymbolEnd+10000)+100
    PosBody2=data.find("picked",PosBody1,PosBody1+10000)

    PosScanning=PosBody2 +100   
    GlobalList.append(List) 



#Opening Link to retrieve information
print("Opning links to retrieve detailed information form CC_transcript")
j=1
for item in GlobalList:
    print("Retrieving data: " +str(j)+"/"+str(len(GlobalList)))
    driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')#open tab
    driver.get(item[3]) 
    html_source2 = driver.page_source
    data2 = str(html_source2.encode('utf-8'))
    #text of CC_transcript
    TextePos=data2.find("$(\"#meaning\").popover();")
    item[6] = data2[TextePos+40:TextePos+1000].replace(","," ")  
    #price of Shares
    BuyPos=item[6].find("place at")+10
    BuyPosend=item[6][BuyPos:].find("share")+BuyPos +6
    item[4]=item[6][BuyPos:BuyPosend]  
    #date
    DatePos=item[6].find(" on ")
    DatePosEnd=item[6][DatePos:].find(".")+DatePos
    item[5]=item[6][DatePos+4:DatePosEnd] 
    j=j+1
driver.close()  

#output of final data   
print("Writting data in .csv file")
f = open('stocktwits.csv','w')
f.write("Ticker")
f.write(' , ')
f.write("Nb of shares")
f.write(' , ')
f.write("Text of stocktwits")
f.write(' , ')
f.write("Link")
f.write(' , ')
f.write("Price of buying")
f.write(' , ')
f.write("Date")
f.write(' , ')
f.write("Text of CC_transcript")
f.write('\n')
for item in GlobalList:
    for elem in item:
        f.write(elem)
        f.write(' , ')# excel change of column
    f.write('\n')   # excel change of line
f.close()

time.sleep(5)   
print("Done")
import pytwits


def main():

    access_token = 'TOKEN'
    stocktwits = pytwits.StockTwits(access_token=access_token)

    watchlist = stocktwits.watchlists(path='show', id='WL_ID_HERE')
    print('\n\n'.join([symbol['symbol'] for symbol in watchlist.symbols]))


if __name__ == '__main__':
    main()