【发布时间】:2016-01-09 04:58:17
【问题描述】:
我最近一直在使用newspaper 库。我发现的唯一问题是当我做article.publish_date 时,我总是得到None。
class NewsArticle:
def __init__(self,url):
self.article = Article(url)
self.article.download()
self.article.parse()
self.article.nlp()
def getKeywords(self):
x = self.article.keywords
for i in range(0,len(x)):
x[i] = x[i].encode('ascii', 'ignore')
return x
return self.article.keywords
def getSummary(self):
return self.article.summary.encode('ascii', 'ignore')
def getAuthors(self):
x = self.article.authors
for i in range(0,len(x)):
x[i] = x[i].encode('ascii', 'ignore')
return x
def thumbnail_url(self):
return self.article.top_image.encode('ascii', 'ignore')
def date_made(self):
print self.article.publish_date
return self.article.publish_date
def get_videos(self):
x=self.article.movies
for i in range(0,len(x)):
x[i] = x[i].encode('ascii', 'ignore')
return x
def get_title(self):
return self.article.title.encode('ascii','ignore')
我正在浏览一堆 URL。你可以看到我在返回之前打印出publish_date。
正如我之前所说的那样:
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
所有其他功能都按预期工作。该站点的文档查看了一个示例,
>>> article.publish_date
datetime.datetime(2013, 12, 30 0, 0)
我很确定我正在这样做。我不确定是否有人注意到我的问题。
【问题讨论】:
-
您遇到问题的网址是什么?
-
所有网址均未返回任何发布日期。
-
@Eigenvalue 不要忘记
article.parse()之前article.publish_date -
@Eigenvalue 哦,我认为你有一个订单问题,所以将
article.publish_date值分配给__init__中的一个实例变量并在任何你想要的地方使用它。 -
我在 date_made 函数中做了这个。为什么它必须在 init 中?
标签: python datetime python-newspaper