本文整理汇总了Python中feedparser.FeedParserDict方法的典型用法代码示例。如果您正苦于以下问题:Python feedparser.FeedParserDict方法的具体用法?Python feedparser.FeedParserDict怎么用?Python feedparser.FeedParserDict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类feedparser
的用法示例。
在下文中一共展示了feedparser.FeedParserDict方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compose_message
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def compose_message(item: feedparser.FeedParserDict) -> str:
"""Compose a tweet from an RSS item (title, link, description)
and return final tweet message.
Parameters
----------
item: feedparser.FeedParserDict
An RSS item.
Returns
-------
str
Returns a message suited for a Twitter status update.
"""
title, link, _ = item["title"], item["link"], item["description"]
message = shorten_text(title, maxlength=250) + " " + link
return message
示例2: encode_feedparser_dict
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def encode_feedparser_dict(d):
"""
helper function to get rid of feedparser bs with a deep copy.
I hate when libs wrap simple things in their own classes.
"""
if isinstance(d, feedparser.FeedParserDict) or isinstance(d, dict):
j = {}
for k in d.keys():
j[k] = encode_feedparser_dict(d[k])
return j
elif isinstance(d, list):
l = []
for k in d:
l.append(encode_feedparser_dict(k))
return l
else:
return d
示例3: encode_feedparser_dict
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def encode_feedparser_dict(self, d):
"""
helper function to get rid of feedparser bs with a deep copy.
I hate when libs wrap simple things in their own classes.
"""
if isinstance(d, feedparser.FeedParserDict) or isinstance(d, dict):
j = {}
for k in d.keys():
j[k] = self.encode_feedparser_dict(d[k])
return j
elif isinstance(d, list):
l = []
for k in d:
l.append(self.encode_feedparser_dict(k))
return l
else:
return d
示例4: run_query
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def run_query(self, search_query, start=0, max_results=10):
"""Queries url and returns a parsed response
Args:
search_query (string): The terms we are looking for
start (int, optional): Defaults to 0. Start at results page
max_results (int, optional):
Defaults to 10. How many results shall be retrieved
Returns:
FeedParserDict: A parsed dictionary with the information
"""
query = f"search_query={search_query}&sortBy=lastUpdatedDate&start={start}&max_results={max_results}" # pylint: disable=C0301
r = requests.get(self.base_url + query)
parsed_response = feedparser.parse(r.text)
return parsed_response
示例5: _feed
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def _feed(url=URL): # type: (str) -> feedparser.FeedParserDict
"""Cache contents of the feed, so it's only read once"""
if url not in _CACHED_FEEDS:
_CACHED_FEEDS[url] = feedparser.parse(url)
return _CACHED_FEEDS[url]
示例6: test_feeds
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def test_feeds(mock_feedparse, patch_try_shorten):
from plugins import feeds
mock_feedparse.return_value = FeedParserDict(
entries=[],
)
assert feeds.rss('xkcd') == "Feed not found."
mock_feedparse.assert_called_with('http://xkcd.com/rss.xml')
mock_feedparse.reset_mock()
mock_feedparse.return_value = FeedParserDict(
entries=[FeedParserDict(title='foo1', link='http://example.com')],
feed=FeedParserDict(title='test'),
)
with_title = "\x02test\x02: foo1 (http://example.com)"
assert feeds.rss('http://rss.example.com/feed.xml') == with_title
mock_feedparse.assert_called_with('http://rss.example.com/feed.xml')
mock_feedparse.reset_mock()
mock_feedparse.return_value = FeedParserDict(
entries=[FeedParserDict(title='foo1', link='http://example.com')],
feed=FeedParserDict(),
)
without_title = "foo1 (http://example.com)"
assert feeds.rss('http://rss.example.com/feed.xml') == without_title
mock_feedparse.assert_called_with('http://rss.example.com/feed.xml')
mock_feedparse.reset_mock()
示例7: _get_feed_home_url
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def _get_feed_home_url(self, feed: feedparser.FeedParserDict) -> str:
link = feed.feed.get("link") or ''
if not link.startswith('http') and not link.startswith('/'):
# 有些link属性不是URL,用author_detail的href代替
# 例如:'http://www.cnblogs.com/grenet/'
author_detail = feed.feed.get('author_detail')
if author_detail:
link = author_detail.get('href')
return link
示例8: _get_feed_title
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def _get_feed_title(self, feed: feedparser.FeedParserDict) -> str:
return feed.feed.get("title") or \
feed.feed.get("subtitle") or \
feed.feed.get("description")
示例9: list
# 需要导入模块: import feedparser [as 别名]
# 或者: from feedparser import FeedParserDict [as 别名]
def list(self, filters=None):
"""Apply filters on entries and return a filtered list
filters (dict, optional): Defaults to None. Parameters to filter entries
Returns:
list: List of arxiv document objects
"""
if not filters:
return self._entries
self._entries = self.fetch_papers()
result = []
result.extend(self._entries)
for key, value in filters.items():
result = [e for e in result if self._check(e, key, value)]
return [ad.ArxivDocument.from_dict(r) for r in result]
# @classmethod
# def encode_feedparser_dict(cls, fp_dict):
# """
# recursive function to convert the internal feedparse object to a simple dict
# """
# if isinstance(fp_dict, feedparser.FeedParserDict) or isinstance(fp_dict, dict):
# ret_dict = {}
# for key in fp_dict.keys():
# ret_dict[key] = self.encode_feedparser_dict(fp_dict[key])
# return ret_dict
# elif isinstance(fp_dict, list):
# dict_list = []
# for key in fp_dict:
# dict_list.append(self.encode_feedparser_dict(key))
# return dict_list
# else:
# return fp_dict