我使用python get(url)命令获取HTML数据,该命令返回包含“\n”字符的原始HTML数据。当我对此运行replace(“\n”,”“)命令时,它不会删除它。有人能解释一下如何在“simple\u get”阶段或者从“raw\u htmlB”阶段删除它吗!代码如下。你知道吗
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)
我相信您需要在\n中添加另一个反作用“\”,以便搜索文本字符串\n并避免反作用。你知道吗
快速示例:
退货:
而:
仅返回:
我认为在双引号之间加一个空格对你有好处
使用原始字符串
r'\n
或者记住\n
代表换行符,您需要转义反斜杠:.replace('\\n', '')
相关问题 更多 >
编程相关推荐