.find() does not take a string? Python 3.6 -
i have lately been working on project retrieves metar noaa website, , slices metar data , prints it. have encountered problem change code python3.6
, when try .find()
marker sates start of metar data gives me error message:
file "/users/mrzeus/desktop/py3.6_project/version_1.py", line 22, in damainprogram data_start = website_html.find("<--data_start-->") typeerror: bytes-like object required, not 'str'
i understand error saying. means .find()
not take string, according python docs .find()
function take string!
here section of code having trouble with:
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 undecoded metar data:\n"+metar_slice)
httpresponce.read()
returns bytes
object. bytes
methods (such .find
) require arguments of type bytes
.
you either change br1_string
bytes
object:
br1_string = b"<!-- data starts here -->"
or, decode response:
website_html = website.read().decode()
Comments
Post a Comment