为什么 BeautifulSoup 没有在 HTML 中显示标题
Why BeautifulSoup is not showing the title in HTML
我正在制作一个简单的 scraper,看看我是否可以从我制作的 HTML 中获得我需要的输入值。它总是显示 None 作为答案,所以我正在检查更简单的内容,即 html.
中的 < title >
from bs4 import BeautifulSoup # parsing
r = open("C:/Python27/Pruebas/pruebahtml.html")
print(r.read())
soup = BeautifulSoup(r,"html.parser")
title = soup.title
print(title)
r.close()
但我仍然得到 None 作为答案,我也使用了 findALL,find_all 并找到了这样做,但我得到了一些错误。有谁知道我的错误在哪里?
您正在向 bs4 传递一个空字符串,因为 print(r.read())
已将指针移动到文件末尾,删除 print(r.read())
并将其传递给 BeautifulSoup(
或调用 r.seek(0)
并通过。一旦您调用 read、readlines 或迭代文件对象,迭代器就会被消耗掉,因此没有任何内容可读。
我正在制作一个简单的 scraper,看看我是否可以从我制作的 HTML 中获得我需要的输入值。它总是显示 None 作为答案,所以我正在检查更简单的内容,即 html.
中的 < title >from bs4 import BeautifulSoup # parsing
r = open("C:/Python27/Pruebas/pruebahtml.html")
print(r.read())
soup = BeautifulSoup(r,"html.parser")
title = soup.title
print(title)
r.close()
但我仍然得到 None 作为答案,我也使用了 findALL,find_all 并找到了这样做,但我得到了一些错误。有谁知道我的错误在哪里?
您正在向 bs4 传递一个空字符串,因为 print(r.read())
已将指针移动到文件末尾,删除 print(r.read())
并将其传递给 BeautifulSoup(
或调用 r.seek(0)
并通过。一旦您调用 read、readlines 或迭代文件对象,迭代器就会被消耗掉,因此没有任何内容可读。