首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >csv项目问题的web抓取

csv项目问题的web抓取
EN

Stack Overflow用户
提问于 2019-12-05 03:37:23
回答 1查看 52关注 0票数 0

我正试着制作一个应用程序,它能刮掉我最喜欢的十个空间相关的股票价格。但

  • 我的代码有点问题,我刚开始抓取代码。
  • 一旦我开始工作,我想把它放到csv文件
  • 中,并用它制作一个条形图,我很想得到一些帮助,我在Anaconda :

中这样做。

我的代码:

代码语言:javascript
运行
复制
from bs4 import BeautifulSoup 
#grequests is a unique library that allows you to use many urls with ease
#must install qrequest in annacode use : conda install -c conda-forge grequests
#if you know a better way to do this, please let me know
import grequests

#scraping my top ten favorite space companies, attempted to pick companies with pure play interest in space


urls = ['https://finance.yahoo.com/quote/GILT/', 'https://finance.yahoo.com/quote/LORL?p=LORL&.tsrc=fin-srch', 'https://finance.yahoo.com/quote/I?p=I&.tsrc=fin-srch' , 'https://finance.yahoo.com/quote/VSAT?p=VSAT&.tsrc=fin-srch', 'https://finance.yahoo.com/quote/RTN?p=RTN&.tsrc=fin-srch', 'https://finance.yahoo.com/quote/UTX?ltr=1', 'https://finance.yahoo.com/quote/TDY?ltr=1', 'https://finance.yahoo.com/quote/ORBC?ltr=1', 'https://finance.yahoo.com/quote/SPCE?p=SPCE&.tsrc=fin-srch', 'https://finance.yahoo.com/quote/BA?p=BA&.tsrc=fin-srch',]  
unsent_request = (grequests.get(url) for url in urls)

results = grequests.map(unsent_request)


def  parsePrice():
    soup = BeautifulSoup(r.text,"html")
    price=soup.find_all('div',{'class':'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="52">4.1500'})[0].find('span').text
    return price

    #Trying to figure out how to add this 
    # Random delay
    duration = random.uniform(1, 4)
    print('Random delay for %.2f seconds' % duration)
    time.sleep(duration)

    print('Scraping website for', urls)
    urls = hrefs[stock]
    soup = BeautifulSoup(page.text, 'html.parser')

在此之后,我得到了一个错误:

代码语言:javascript
运行
复制
File "<ipython-input-17-834b9d6c20e5>", line 3
    duration = random.uniform(1, 4)
    ^
IndentationError: unexpected indent

while True:
    print('current stock price: '+str(parsePrice()))

#add to csv file 
df_indu = pd.DataFrame(
    L['Top Ten Space Stocks'],
    columns=['stock name', 'stock price', 'date of listing'])
df_indu.to_csv('spacestocks.csv', index=False, sep='|')

这是我到目前为止的整个项目。我试图添加一个时间延迟和刮去所有这些股票的价格放在一个csv文件,以图表.谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-12-05 03:41:01

您的代码缩进您的parsePrice()函数定义。在函数的末尾,您将状态为return price,从而结束函数定义。现在,不应该再缩进下面的代码。这就是为什么在那之后的每件事都会出现意想不到的缩进错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59187879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档