繁体   English   中英

网络抓取到 csv 项目问题

[英]web scraping to csv project problems

我正在尝试制作一个应用程序,它可以抓取我最喜欢的与空间相关的股票价格的前十名。

  • 我的代码有一些问题,而且我是抓取的新手。
  • 一旦我开始工作,我想把它放到一个 csv 文件中
  • 并用它制作条形图,我希望得到一些帮助和建议。
  • 我也在 Anaconda 中这样做:

我的代码:

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')

在此之后,我收到此错误:

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 文件中以绘制图形......谢谢

您的代码在parsePrice()函数定义处缩进。 在您的函数结束时,您声明return price从而结束函数定义。 现在您的以下代码不应再缩进。 这就是为什么之后的所有内容都会出现意外的缩进错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM