错误:
AttributeError:“NoneType”对象没有属性“find_all”。
我尝试修复它,但没有成功。
def show_homework_list(link, page, session, header) -> None:
hw_response = session.get(link, headers=header).text
hw_soup = BeautifulSoup(hw_response, "lxml")
hw_content = hw_soup.find("div", id="content")
hw_table = hw_soup.find("table", class_="grid gridLines vam hmw")
hw_info = hw_table.find_all("tr")
os.system("cls")
for i in range(1, len(hw_info)):
lg = hw_info[i].find("td", class_="light").text.strip()
br = hw_info[i].find("td", class_="breakword").text.strip()
nw = hw_info[i].find("td", class_="nowrap").text.strip()
text = f"\n{i}) {lg} | {nw.split('\n')[0]}\n{br}\n"
print(text)
print("-" * 40)
print(f"\n<-- {page} -->\n")
您的 HTML 响应不包含带有类
table
、grid
、gridLines
和 vam
的标签 hmw
。如果是这样,那么您的代码就可以工作。
假设有以下 HTML 内容:
hw_response = '''\
<div id="content">
<table class="grid gridLines vam hmw">
<tr><td>Hello</td></tr>
<tr><td>World</td></tr>
</table>
</div>
'''
hw_soup = BeautifulSoup(hw_response, "lxml")
hw_content = hw_soup.find("div", id="content")
hw_table = hw_soup.find("table", class_="grid gridLines vam hmw")
hw_info = hw_table.find_all("tr")
输出:
>>> hw_info
[<tr><td>Hello</td></tr>,
<tr><td>World</td></tr>]