[Solved] Python String Between [closed]


If you look at what is being downloaded, you can see that the data is encoded in UTF-8.

Just add the decode('UTF-8') method to convert the download to something Python 3 can work with:

import urllib.request

url="http://www.bloomberg.com/quote/PLUG:US"

sock = urllib.request.urlopen(url).read().decode('UTF-8')
print(sock.count("data_values"), sock.count("show_1D"))
# 1 1
string2=sock.replace("data_values","show_1D")
print (string2.count("data_values"), string2.count("show_1D"))
# 0 2

While that may solve this one issue, do use an HTML parser rather than simple regex’s or string replaces to deal with XML and HTML such as beautiful soup among many others.

solved Python String Between [closed]