本文整理汇总了Python中inc.Commons.Commons.load_url_string方法的典型用法代码示例。如果您正苦于以下问题:Python Commons.load_url_string方法的具体用法?Python Commons.load_url_string怎么用?Python Commons.load_url_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inc.Commons.Commons
的用法示例。
在下文中一共展示了Commons.load_url_string方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_load_url_string
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import load_url_string [as 别名]
def test_load_url_string(self):
url1 = "https://httpbin.org/get"
data1 = Commons.load_url_string(url1)
data1split = data1.split("\n")
assert data1split[0] == "{", "First line incorrect."
assert data1split[1] == " \"args\": {}, ", "String response incorrect."
url2 = "https://httpbin.org/headers"
headers2 = [["User-Agent", "Example data"]]
data2 = Commons.load_url_string(url2, headers2)
data2split = data2.split("\n")
assert data2split[0] == "{", "First line incorrect."
assert data2split[1] == " \"headers\": {", "String response incorrect."
assert "\"User-Agent\": \"Example data\"" in data2, "Headers missing from request."
示例2: check_feed
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import load_url_string [as 别名]
def check_feed(self):
"""
Checks the feed for any updates
:return: list of ElementTree XML elements
"""
rss_data = Commons.load_url_string(self.url)
rss_elem = ElementTree.fromstring(rss_data)
channel_elem = rss_elem.find("channel")
new_items = []
# Update title
title_elem = channel_elem.find("title")
self.title = title_elem.text
# Loop elements, seeing when any match the last item's hash
latest_hash = None
for item_elem in channel_elem.findall("item"):
item_xml = ElementTree.tostring(item_elem)
item_hash = hashlib.md5(item_xml).hexdigest()
if latest_hash is None:
latest_hash = item_hash
if item_hash == self.last_item_hash:
break
new_items.append(item_elem)
# Update last item hash
self.last_item_hash = latest_hash
self.last_check = datetime.now()
# Return new items
return new_items
示例3: run
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import load_url_string [as 别名]
def run(self, line, user_obj, destination_obj=None):
api_key = user_obj.get_server().get_hallo().get_api_key("thecatapi")
if api_key is None:
return "No API key loaded for cat api."
url = "http://thecatapi.com/api/images/get?format=xml&api_key=" + api_key + "&type=gif"
xml_string = Commons.load_url_string(url)
doc = minidom.parseString(xml_string)
cat_url = doc.getElementsByTagName("url")[0].firstChild.data
return cat_url
示例4: get_rss_data
# 需要导入模块: from inc.Commons import Commons [as 别名]
# 或者: from inc.Commons.Commons import load_url_string [as 别名]
def get_rss_data(self):
headers = None
# Tumblr feeds need "GoogleBot" in the URL, or they'll give a GDPR notice
if "tumblr.com" in self.url:
headers = [["User-Agent", "Hallo IRCBot [email protected] (GoogleBot/4.5.1)"]]
# Actually get the data
rss_data = Commons.load_url_string(self.url, headers)
# PHDComics doesn't always escape ampersands correctly
if "phdcomics" in self.url:
rss_data = rss_data.replace("& ", "& ")
# Chainsaw suit has a blank first line
if "chainsawsuit" in self.url and rss_data.startswith("\r\n"):
rss_data = rss_data[2:]
return rss_data