Я боролся с этим кодом:
def MainPageSpider(max_pages):
page = 1
while page <= max_pages:
url = 'url' + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = bs(plain_text, 'html.parser')
for link in soup.findAll(attrs={'class':'col4'}):
href = 'url' + link.a['href']
title = link.span.text
PostPageItems(href)
page += 1
def PostPageItems(post_url):
source_code = requests.get(post_url)
plain_text = source_code.text
soup = bs(plain_text, 'html.parser')
for items in soup.findAll(attrs={'class':'container'}):
title2 = items.find('h1', {'class':'title'}).get_text()
print(title2)
MainPageSpider(1)
Каждый раз, когда я пытаюсь получить текст из 'h1', я получаю эту ошибку:
Traceback (most recent call last):
File "Xfeed.py", line 33, in <module>
MainPageSpider(1)
File "Xfeed.py", line 17, in MainPageSpider
PostPageItems(href)
File "Xfeed.py", line 27, in PostPageItems
test = title2.get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'
Но когда я запускаюбез 'get_text ()' я получу HTML 'h1':
<h1 class="title">Title 1</h1>
None
None
None
None
<h1 class="title">Title 2</h1>
None
None
None
None
<h1 class="title">Title 3</h1>
None
None
None
None
Я не очень понимаю, почему эта ошибка, в то время как с title = link.span.text
у меня нет проблем с получением текста.Я хочу только текст.