抓取最后的“下一页”后,BeautifulSoup AttributeError

时间:2019-11-04 22:16:12

标签: python beautifulsoup attributeerror

它遍历所有20个“下一页”,但最后没有命中“ else”语句以表示不再有页面,并引发以下内容:

第38行(if语句所在的位置)上的“ AttributeError:'NoneType'对象没有属性'get'”。

while True:
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
blabbermouth_articles = soup.find_all('article', {'class': 'category-news'})
# print(response)

for blabbermouth_article in blabbermouth_articles:
    image = blabbermouth_article.find('img').get('src')
    title = blabbermouth_article.find('a').get('title')
    shortLink = blabbermouth_article.find('a').get('href')
    print(title)
    link = "https://www.blabbermouth.net" + shortLink
    print(link)
    print(image)

    article_no += 1
    all_blabbermouth_articles[article_no] = [image, title, link]

next_page_tag = soup.find('a', {'class': 'next_page'})
if next_page_tag.get('href'):
    url = 'https://www.blabbermouth.net' + next_page_tag.get('href')
    print(url)
else:
    print("END OF LAST PAGE")
    break

0 个答案:

没有答案