正则表达式把引号变成奇怪的符号

2024-10-17 08:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从下面的tweet中删除文本。你知道吗

.@mikhailaleshin on drivers scared of the #Indy500: "They just have small **. ... That’s the only explanation." - 

我正在对网站的源代码执行正则表达式:

page = urllib.urlopen(url).read()
soup = BeautifulSoup(page, "html.parser")
soup_string = str(soup)
tweet_text = re.search(ur'<title*?>(.*)</title>', soup_string).group(1)

但当我把它打印到我的屏幕上时,我得到了:

.@mikhailaleshin on drivers scared of the #Indy500: "They just have small **. ... ThatÔÇÖs the only explanation."

所以引号变成了ÔÇÖ。我的最佳选择是,这是某种编码问题,但我不知道如何解决它。你知道吗


Tags: oftheonlythatonhavetweetsmall
1条回答
网友
1楼 · 发布于 2024-10-17 08:37:00
re.search(ur'<title*?>(.*)</title>', soup_string, re.U).group(1)

(或)

re.search(ur'<title*?>(.*)</title>', soup.enocde('utf-8'), re.U).group(1)

如果是unicode错误,那么上面的一个应该解决这个错误。你知道吗

这是一个解决办法

url = "https://twitter.com/a_s12/status/865229374844481536"
page = urllib.urlopen(url).read()
soup = BeautifulSoup(page, "html.parser")
soup_string = soup.findAll('title')[0].encode('utf=8')
tweet_text = re.search(ur'<title>(.*?)</title>', soup_string, re.U).group(1)
print tweet_text

相关问题 更多 >