I am getting HTML data with a python get( url ) command which returns raw HTML data that contains “\n” characters. When I run the replace (“\n”,””) command against this it does not remove it. Could some explain how to either remove this at the "simple_get" stage or from the "raw_htmlB" stage! Code below.
from CodeB import simple_get
htmlPath = "https://en.wikipedia.org/wiki/Terminalia_nigrovenulosa"
raw_html = simple_get(htmlPath)
if raw_html is None:
print("not found")
else:
tmpHtml = str(raw_html)
tmpHtmlB = tmpHtml.replace("\n","")
print("tmpHtmlB:=", tmpHtmlB)
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
def simple_get(url):
try:
with closing(get(url, stream=True)) as resp:
if is_good_response(resp):
return resp.content
else:
return None
except RequestException as e:
log_error('Error during requests to {0} : {1}'.format(url, str(e)))
return None
def is_good_response(resp):
content_type = resp.headers['Content-Type'].lower()
return (resp.status_code == 200
and content_type is not None
and content_type.find('html') > -1)
def log_error(e):
print(e)