gpt4 book ai didi

python - 将 ' 变为普通字符

转载 作者:太空狗 更新时间:2023-10-29 14:47:44 25 4
gpt4 key购买 nike

我在显示内容时遇到问题,我的程序:

#! /usr/bin/python

import urllib
import re

url = "http://yahoo.com"
pattern = '''<span class="medium item-label".*?>(.*)</span>'''

website = urllib.urlopen(url)
pageContent = website.read()
result = re.findall(pattern, pageContent)

for record in result:
print record

输出:

Masked teen killed by dad
First look in &#39;Hotel of Doom&#39;
Ex-NFL QB&#39;s sad condition
Reporter ignores warning
Romney&#39;s low bar for debates

所以问题是我应该在我的代码中包含什么才能将 ' 转换为字符

最佳答案

在 Python2 中:

In [16]: text = 'Ex-NFL QB&#39;s sad condition'

In [17]: import HTMLParser

In [18]: parser = HTMLParser.HTMLParser()

In [19]: parser.unescape(text)
Out[19]: u"Ex-NFL QB's sad condition"

在 Python3 中:

import html.parser as htmlparser
parser = htmlparser.HTMLParser()
parser.unescape(text)

关于python - 将 &#39 变为普通字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12646177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com