Web抓取名称-find_all上的NoneType错误

时间:2018-11-07 19:31:54

标签: python beautifulsoup

我正在尝试使用BeautifulSoup提取名称(通知程序)。但是当我测试运行它会给出一个NoneType错误: AttributeError: 'NoneType' object has no attribute 'find_all'

代码:

import requests
from bs4 import BeautifulSoup


page_counter = 1


while page_counter < 5:
    print('page number: %d' %page_counter)
    url = requests.get('http://zone-h.org/archive/page=%d'%page_counter,timeout=8).text()

soup = BeautifulSoup(url, 'html.parser')
table = soup.find('table')
rows = table.find_all('tr')

1 个答案:

答案 0 :(得分:2)

问题出在这两行:

table = soup.find('table')
rows = table.find_all('tr')

soup.find('table')如果找不到带有标签None的任何元素,则将返回table。这将导致table.find_all('tr')返回您收到的错误,因为在上一行中将table分配给了None

这有意义吗?