<code>main.route(
'/newspaper'
)
def newspaper():
articles = []
# category = Category.query.filter_by(tag=
'最新消息'
).first()
# posts = Post.query.filter_by(category=category).order_by(Post.timestamp.desc()).limit(5)
# 可以抓取到内容
article = extract_article(
'http://www.xcb.sdnu.edu.cn/info/1014/11211.htm'
)
# 无法抓取到内容
article = extract_article(
'http://127.0.0.1:5000/post/132'
)
articles.append(article)
""
"
for
post in posts:
# URL 不对
post_url =
'http://127.0.0.1:5000/post/'
+ str(post.id)
article = extract_article(post_url=post_url)
if
article:
article[
'post_url'
] = post_url
articles.append(article)
""
"
return
render_template(
'newspaperDemo.html'
, articles=articles)
def extract_article(post_url):
article = Article(post_url)
print
(post_url)
print
(
'-------------------------------'
)
article.download()
print
(
'===================================='
)
article.parse()
print
(
'*******************************'
)
title = article.title
img = article.top_image
return
{
'title'
: title,
'img'
: img,
}
</code>