2017-07-25 89 views
0

我最近一直在研究一個項目,它從NOAA網站獲取METAR,並切割METAR數據並打印它。現在我遇到了問題,更改代碼以Python3.6,當我嘗試.find()那心滿意足的METAR數據的開始它給我這個錯誤消息的標記:.find()不帶字符串? Python 3.6

File "/Users/MrZeus/Desktop/PY3.6_PROJECT/version_1.py", line 22, in daMainProgram 
    data_start = website_html.find("<--DATA_START-->") 
TypeError: a bytes-like object is required, not 'str' 

我明白這是什麼錯誤說法。這意味着.find()不接受字符串,但根據python文檔.find()函數確實需要一個字符串!

這裏是我遇到的麻煩的一段代碼:

website = urllib.request.urlopen(airid) 

website_html = website.read() 

print(website_html) 

br1_string = "<!-- Data starts here -->" 

data_start = website_html.find(br1_string) 

br1 = data_start + 25 

br2 = website_html.find("<br />") 

metar_slice = website_html[br1:br2] 

print("Here is the undecoded METAR data:\n"+metar_slice) 

回答

1

HTTPResponce.read()返回一個bytes對象。 bytes方法(如.find)需要參數類型bytes

你可以要麼改變br1_stringbytes對象:

br1_string = b"<!-- Data starts here -->" 

或解碼響應:

website_html = website.read().decode() 
+0

謝謝sooooooo多! –