Я пытался создать веб-скребок, чтобы помочь мне не отставать от статей, опубликованных в моей отрасли.
Я в своем уме, потому что когда я пытаюсь запустить свой код через Flask,Я продолжаю получать эту ошибку:
TypeError: Функция просмотра не вернула правильный ответ. Функция либо вернула None, либо завершилась без оператора return.
Вот код, выдающий ошибку:
Doc 1 - это blogscraper.py и читает:
import requests
from bs4 import BeautifulSoup
def blog_parser(url) -> 'html':
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
result = requests.get(url, headers=headers)
return result.content
def html(url) -> 'html':
website = blog_parser(url)
html = BeautifulSoup(website, 'html.parser')
return html
def site_articles(url, element, unique_element) -> 'html':
sitehtml = html(url)
article_data = sitehtml.find_all(element, unique_element)
return article_data
def skillsoft_titles(list_item):
skillsoft_articles = site_articles('https://www.skillsoft.com/blog', "h1", {"class": "entry-title"})
entries = skillsoft_articles[list_item]
title = entries.find('a').get_text()
return title
def skillsoft_link(list_item):
skillsoft_articles = site_articles('https://www.skillsoft.com/blog', "h1", {"class": "entry-title"})
entries = skillsoft_articles[list_item]
link = entries.find('a').get('href')
return link
def skillsoft_description(list_item):
skillsoft_articles = site_articles('https://www.skillsoft.com/blog', "div", {"class": "entry-content"})
entries = skillsoft_articles[list_item]
description = entries.select_one("div p:nth-of-type(2)").text
return description
def opensesame_titles(list_item) -> str:
opensesame_articles = site_articles('https://www.opensesame.com/site/blog/', "div", {"class": "blog-post-right"})
entries = opensesame_articles[list_item]
title = entries.find('a').get_text()
return title
def opensesame_link(list_item) -> str:
opensesame_articles = site_articles('https://www.opensesame.com/site/blog/', "div", {"class": "blog-post-right"})
entries = opensesame_articles[list_item]
link = entries.find('a').get('href')
return link
def opensesame_description(list_item):
opensesame_articles = site_articles('https://www.opensesame.com/site/blog/', "section", {"class": "entry-content"})
entries = opensesame_articles[list_item]
description = entries.find('p').text
return description
def cornerstone_titles(list_item) -> str:
cornerstone_articles = site_articles('https://www.cornerstoneondemand.com/rework', "h2", {"class": "text-blue"})
entries = cornerstone_articles[list_item]
title = entries.find('a').get_text()
return title
def cornerstone_link(list_item) -> str:
cornerstone_articles = site_articles('https://www.cornerstoneondemand.com/rework', "h2", {"class": "text-blue"})
entries = cornerstone_articles[list_item]
link = entries.find('a').get('href')
return link
def cornerstone_description(list_item) -> str:
cornerstone_articles = site_articles('https://www.cornerstoneondemand.com/rework', "div", {"class": "col3-teaser-cont"})
entries = cornerstone_articles[list_item]
description = entries.find('p').text
return description
def print_values(list_item, title_func, link_func, desc_func):
return (print('Title:', title_func(list_item), '\n' 'Link:', link_func(list_item), '\n' 'Description:', desc_func(list_item)))
Это прекрасно работает само по себе, в pycharm он возвращает именно то, что я хочу.
Doc 2 - это мой документ для колб, а код:
import blogscraper
from flask import Flask
app = Flask(__name__)
skillsoft_titles = blogscraper.skillsoft_titles
skillsoft_link = blogscraper.skillsoft_link
skillsoft_description = blogscraper.skillsoft_description
@app.route('/', methods = ['GET'])
def skillsoft():
output = blogscraper.print_values(1, skillsoft_titles, skillsoft_link, skillsoft_description)
return output
skillsoft()
app.debug = True
app.run()
app.run(debug = True)
Это приводит к ошибке,По какой-то причине это приводит к возврату None or no, что не имеет смысла для меня после поиска в Google ни одного возврата. Любая помощь в этом очень ценится!