本文整理汇总了Python中html.parser.feed方法的典型用法代码示例。如果您正苦于以下问题:Python parser.feed方法的具体用法?Python parser.feed怎么用?Python parser.feed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类html.parser
的用法示例。
在下文中一共展示了parser.feed方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_beta_branch_name
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def get_beta_branch_name(game_name="ets2"):
"""
Get the current required beta branch name to comply with TruckersMP.
If downgrade is needed, this returns a branch name that can be used
to install TruckersMP-compatible versions of games (e.g. "temporary_1_36")
on success or None on error.
If downgrade is not needed, this returns None.
"""
try:
parser = DowngradeHTMLParser()
with urllib.request.urlopen(URL.truckersmp_stats) as f:
parser.feed(f.read().decode("utf-8"))
if parser.data[game_name]:
version = get_supported_game_versions()[game_name].split(".")
return "temporary_{}_{}".format(version[0], version[1])
return None
except Exception:
return None
示例2: github_contrib
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def github_contrib(user, year):
""" Get GitHub user daily contribution """
# Check for a cached version (file)
filename = "github-{0}-{1}.html".format(user, year)
if os.path.exists(filename):
with open(filename) as file:
contents = file.read()
# Else get file from GitHub
else:
url = "https://github.com/users/{0}/contributions?to={1}-12-31"
url = url.format(user, year)
contents = str(urllib.request.urlopen(url).read())
with open(filename, "w") as file:
file.write(contents)
# Parse result (html)
n = 1 + (date(year, 12, 31) - date(year, 1, 1)).days
C = -np.ones(n, dtype=int)
class HTMLParser(html.parser.HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == "rect":
data = {key:value for (key,value) in attrs}
date = dateutil.parser.parse(data["data-date"])
count = int(data["data-count"])
day = date.timetuple().tm_yday - 1
if count > 0:
C[day] = count
parser = HTMLParser()
parser.feed(contents)
return C
示例3: count
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def count(filename):
if not HtmlWordCounter.can_count(filename):
return 0
parser = HtmlWordCounter.__HtmlParser()
with open(filename, encoding="utf-8") as file:
parser.feed(file.read())
return parser.count
示例4: _run_check
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def _run_check(self, source, expected_events, collector=None):
if collector is None:
collector = self.get_collector()
parser = collector
for s in source:
parser.feed(s)
parser.close()
events = parser.get_events()
if events != expected_events:
self.fail("received events did not match expected events" +
"\nSource:\n" + repr(source) +
"\nExpected:\n" + pprint.pformat(expected_events) +
"\nReceived:\n" + pprint.pformat(events))
示例5: test_convert_charrefs_dropped_text
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def test_convert_charrefs_dropped_text(self):
# #23144: make sure that all the events are triggered when
# convert_charrefs is True, even if we don't call .close()
parser = EventCollector(convert_charrefs=True)
# before the fix, bar & baz was missing
parser.feed("foo <a>link</a> bar & baz")
self.assertEqual(
parser.get_events(),
[('data', 'foo '), ('starttag', 'a', []), ('data', 'link'),
('endtag', 'a'), ('data', ' bar & baz')]
)
示例6: _parse_error
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def _parse_error(self, source):
def parse(source=source):
parser = self.get_collector()
parser.feed(source)
parser.close()
with self.assertRaises(html.parser.HTMLParseError):
with self.assertWarns(DeprecationWarning):
parse()
示例7: serialize
# 需要导入模块: from html import parser [as 别名]
# 或者: from html.parser import feed [as 别名]
def serialize(result):
"""For a given Met result, map that to our database"""
imageinfos = result['ImageInfo']
thumbnail = None
url = None
for info in imageinfos:
if info['PrimaryDisplay']:
# Use this one
thumbnail = ENDPOINT_BASE_IMAGE_URL + info['Thumbnail']
url = ENDPOINT_BASE_IMAGE_URL + info['LargeWebsite']
break
if not url:
log.warning("Did not get an image URL for %s", result)
return
image = models.Image(url=url)
image.provider = PROVIDER_NAME
image.source = SOURCE_NAME
# Creator might be a few fields
tombstone = result['Tombstone']
creator_names = []
for t in tombstone:
if t['Name'] in CREATOR_LABELS:
val = t['Value']
parser = CreatorParser()
parser.feed(val)
creator_names.append(" ".join(parser.out))
if len(creator_names) > 0:
image.creator = ", ".join(creator_names)
image.thumbnail = thumbnail
image.license = "cc0"
image.license_version = '1.0'
image.foreign_identifier = result['CollectionObject']['CRDID']
image.foreign_landing_url = FOREIGN_LANDING_BASE_URL + str(image.foreign_identifier)
image.title = result['CollectionObject']['Title']
image.identifier = signals.create_identifier(image.url)
image.last_synced_with_source = timezone.now()
try:
image.save()
log.info("Adding image %s-%s (%s) identifier %s", image.title, image.creator, image.foreign_identifier, image.identifier)
except IntegrityError as e:
log.warn(e)
pass
return image