【问题标题】:BeautifulSoup - why is it printing file path and not the contentBeautifulSoup - 为什么打印文件路径而不是内容
【发布时间】:2013-10-17 14:09:46
【问题描述】:

我正在尝试了解 BeautifulSoup 的工作原理。请注意,我对 Python 很陌生,所以我可能遗漏了一些东西。

我打开一个 Python 终端并这样写:

from bs4 import BeautifulSoup
import re
ytchannel = '/home/XXX/Documents/test2'
soup = BeautifulSoup(ytchannel)
print(soup.prettify())

这是我得到的:

<html>
 <body>
  <p>
   /home/XXX/Documents/test2
  </p>
 </body>
</html>

为什么?对我来说完全是无稽之谈。我只想要test2 的内容。 我写的正是 BeautifulSoup 网站上的内容。

【问题讨论】:

    标签: python html beautifulsoup filepath


    【解决方案1】:

    您将字符串传递给BeautifulSoup();确定它是一个文件名,但BeautifulSoup() 不会为您打开文件名。它只对字符串或打开的文件对象进行操作。

    先打开文件; BeautifulSoup() 将读取文件对象,如果您传入这些对象:

    with open(ytchannel) as infile:
        soup = BeautifulSoup(infile)
    

    Making the soup

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多