【发布时间】:2017-11-03 21:37:22
【问题描述】:
我对如何在 python 中转义字符有点困惑。我正在使用 BeautifulSoup 解析一些 HTML,当我检索文本内容时,它看起来像这样:
\u00a0\n\n\n\r\nState-of-the-art security and 100% uptime SLA.\u00a0\r\n\n\n\r\nOutstanding support
我希望它看起来像这样:
State-of-the-art security and 100% uptime SLA. Outstanding support
下面是我的代码:
self.__page = requests.get(url)
self.__soup = BeautifulSoup(self.__page.content, "lxml")
self.__page_cleaned = self.__removeTags(self.__page.content) #remove script and style tags
self.__tree = html.fromstring(self.__page_cleaned) #contains the page html in a tree structure
page_data = {}
page_data["content"] = self.__tree.text_content()
如何删除那些编码的反斜杠字符?我到处看了看,没有什么对我有用。
【问题讨论】:
-
你怎么知道内容是这样的?您是打印还是保存到文件中?
-
我都做了,结果就是这样
-
这就是你做
print(page_data["content"])时得到的吗?你使用的是 Python 2 还是 Python 3? -
是的,我正在使用 python 3
-
您能做到
print(repr(page_data["content"]))并将输出粘贴到您的问题中吗?
标签: python html python-3.x web-crawler