I do not understand why the following code keeps producing an empty string. I am trying to get the code to extract the contents of the website to a "txt" file, but it just keeps producing an empty string. Is there an error in the code?
import urllib3
import certifi
# Function: Convert information within html document to a text file
# Append information to the file
def html_to_text(source_html, target_file):
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi Bundle
headers={'connection': 'keep-alive', 'user-agent': 'Mozilla/5.0', 'accept-encoding': 'gzip, deflate'},
)
r = http.urlopen('GET', source_html)
print(source_html)
response = r.read().decode('utf-8')
# TODO: Find the problem that keeps making the code produce an empty string
print(response)
temp_file = open(target_file, 'w+')
temp_file.write(response)
source_address = "https://sg.finance.yahoo.com/lookup/all?s=*&t=A&m=SG&r=&b=0"
target_location = "C:\\Users\\Admin\\PycharmProjects\\TheLastPuff\\Source\\yahoo_ticker_symbols.txt"
html_to_text(source_address, target_location)