我正在尝试通过 python 将数据从我的 MacBook 发送到我的 microbit 连接到的 USB 端口。我的python程序传输数据然后通过查看我的microbit背面我看到在发送信息时USB端口旁边有一个小灯在闪烁,所以micro bit正在接收信息但是我为microbit编写的程序赢了'不显示已发送的信息。我也遵循了有关如何执行此操作的教程。出了点问题我需要帮助!
import serial
import Stock_Web as SW
import time
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "/dev/cu.usbmodem14102"
ser.open()
while True:
i =+ 1
i = str(i)
print('this is time ' + i)
DowPerBytes = str(SW.DowPercent())
DowPerBytes = DowPerBytes.encode()
ser.write(DowPerBytes)
time.sleep(.5)
# data = microbitdata[2:]
# data = data.replace('')
这是我的自定义模块 SW:
import requests
from bs4 import BeautifulSoup as soup
def DowPercent():
url = 'https://money.cnn.com/data/markets/'
result = requests.get(url)
src = result.content
Soup = soup(src, 'html.parser')
stock_per_raw = Soup.find('span', class_="ticker-name-change", attrs={"stream": "changePct_599362", "data-ticker-name": "Dow"})
return soup.get_text(stock_per_raw)
这是微位代码: