Обход Loop AttributeError: объект «NoneType» не имеет атрибута «findAll»Python

Программы на Python
Anonymous
 Обход Loop AttributeError: объект «NoneType» не имеет атрибута «findAll»

Сообщение Anonymous »

Код: Выделить всё

import requests
from bs4 import BeautifulSoup
import csv
from urlparse import urljoin
import urllib2

base_url = 'http://www.baseball-reference.com'
data = requests.get("http://www.baseball-reference.com/players/")
soup = BeautifulSoup(data.content)
player_url = 'http://www.baseball-reference.com/players/'
game_logs = 'http://www.baseball-reference.com/players/gl.cgi?id='
years = ['2000','2001','2002','2003','2004','2005','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
url = []
for link in soup.find_all('a'):
if link.has_attr('href'):
base_url + link['href']
url.append(base_url + link['href'])
sink = []
for l in url:
if l[0:42] in player_url:
sink.append(l)
abc = []
for aa in sink:
if len(aa) > 48:
abc.append(aa)
urlz = []
for ab in abc:
data = requests.get(ab)
soup = BeautifulSoup(data.content)
for link in soup.find_all('a'):
if link.has_attr('href'):
urlz.append(base_url + link['href'])
abc = []
for aa in urlz:
if game_logs in aa:
abc.append(aa)
urlll = []
for ab in years:
for ac in abc:
if ab in ac:
urlll.append(ac)

for j in urlll:
response = requests.get(j)
html = response.content
soup = BeautifulSoup(html)
table = soup.find('table', attrs={'id': 'batting_gamelogs'})
list_of_rows = []
for row in table.findAll('tr'):
list_of_cells = []
for cell in row.findAll('td'):
text = cell.text.replace(' ', '').encode("utf-8")
list_of_cells.append(text)
list_of_rows.append(list_of_cells)
print list_of_rows
Когда я перебираю URL-адреса, чтобы получить таблицы, есть URL-адреса, где таблица не существует. Я получаю сообщение об ошибке следующего вида:

Код: Выделить всё

Traceback (most recent call last):
File "py5.py", line 55, in 
list_of_cells.append(text)
AttributeError: 'NoneType' object has no attribute 'findAll'
Есть ли способ продолжить цикл, даже если таблицы нет?

Подробнее здесь: https://stackoverflow.com/questions/306 ... te-findall

Вернуться в «Python»