我正试图使用BeautifulSoup制作一个程序,从谷歌金融中获得当前的比特币价格。这是我的代码:
from sys import stdout
import requests
from bs4 import BeautifulSoup
src = requests.get('https://www.google.com/finance/\
converter?a=1&from=BTC&to=USD&meta=ei\%3DawPAVfG8JYHpmAGevavICw').text
soup = BeautifulSoup(src, 'html.parser')
target = soup.find('span', {'class': 'bld'})
stdout.write(target.string)我希望将比特币价格作为标准输出输出,这样我就可以将比特币价格输入Linux机器上的其他命令,如下所示:
python bitcoin.py | echo当我试图使用stdout.write()实现这个时,它给了我错误:
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe我需要这样做的原因是,我可以将它添加到我的bash_profile中,以便在每次bash启动时打印当前的比特币价格。
发布于 2015-08-05 02:53:50
管道坏了,因为echo不接受任何输入。错误信息有点令人困惑,我承认:
$ use_bs.py | echo
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
$ use_bs.py | cat
286.0200 USD$ 注意,在美元和下一个提示符之间没有空格,因为您没有添加换行符。你可以用打印代替--默认情况下会添加一个换行符,但是如果你在打印,你可能不想要它。
https://stackoverflow.com/questions/31799546
复制相似问题