首页 > 解决方案 > Beautiful Soup 为什么不返回内容?

问题描述

我正在使用 bs4 来获取一些结果。我可以在源代码中看到 HTML 内容,但是当我尝试使用 bs4 获取它时,它并没有给出而是说“文件不存在”

from bs4 import BeautifulSoup
import requests

source = requests.get("https://result.smitcs.in/grade.php?subid=BA1106")    
soup = BeautifulSoup(source.text, "html.parser")

marks_pre = soup.find("pre")
marks = marks_pre.find("div")

print(marks.prettify())

上面的代码返回

<div style="font-family: courier; line-height: 12px;font-size:
20px;background:white;">  File does not exist </div>

如果我从网上复制源代码并将其保存为本地 HTML 文件然后获取它,则上面的代码可以正常工作。

标签: pythonbeautifulsouppython-requests

解决方案


尝试这个

from bs4 import BeautifulSoup
import requests

URL = "https://result.smitcs.in/grade.php?subid=BA1106"
PAGE = requests.get(URL)

# get HTML content
SOUP = BeautifulSoup(PAGE.content, 'lxml')

marks = SOUP.find("div")

print(marks.prettify())

推荐阅读