【发布时间】:2020-10-20 08:32:54
【问题描述】:
我在 Python 3.8.2 中尝试此代码:
from bs4 import BeautifulSoup
import urllib.request
html = urllib.request.urlopen(
'https://vietnamnet.vn/').read()
soup = BeautifulSoup(html, "html.parser").encode("utf-8")
print(soup.title)
但我收到了:
而不是预期:<title>Báo VietNamNet - Tin tức online, tin nhanh Việt Nam và thế giới</title>
我做错了什么,我该如何解决?
我必须使用 .encode("utf-8") 因为 html 字符串 包含 unicode 字符。对汤有影响吗?
谢谢!
【问题讨论】:
-
title是一个函数,所以你必须调用函数:print(soup.title()),否则你会得到函数对象本身。
标签: python python-3.x beautifulsoup web-crawler python-unicode