我一直在尝试建立一个网络刮板,以帮助我跟上在我的行业发表的文章。你知道吗
我束手无策,因为当我试图通过Flask运行我的代码时,我总是遇到这样的错误:
TypeError:view函数未返回有效响应。函数要么返回None,要么在没有return语句的情况下结束。你知道吗
以下是产生错误的代码:
文件1是博客.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是我的烧瓶Doc,代码是:
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)
这会产生错误。出于某种原因,这会产生一个无回报或无回报,这对我来说没有意义后,谷歌的无回报。非常感谢您的帮助!你知道吗
函数
print_values
返回print(...)
的结果,即None
。这就是我所抱怨的。你知道吗如果删除
print
语句,将返回一个元组:'Title:', title_func(list_item), ...
是一个元组,因为在Python中,用逗号分隔的几个值是一个元组。你知道吗如果flask函数返回一个元组,flask会假定它是一个包含某些元素的元组,例如(response,status),请参见about responses
函数应该返回一个字符串,例如:
或者一个列表也应该是有效的,这取决于你想要实现什么。你知道吗
请注意,同时将所有答案打印到控制台并不是一个好的解决方案,因此您可能希望在了解了print语句的工作原理之后删除它。。。你知道吗
您的
print_values
函数返回print
的返回值,它恰好是None
您需要更改此方法以返回您想要返回的内容。你知道吗
像这样:
相关问题 更多 >
编程相关推荐