本文整理汇总了Python中scraper.Scraper.fetch_url方法的典型用法代码示例。如果您正苦于以下问题:Python Scraper.fetch_url方法的具体用法?Python Scraper.fetch_url怎么用?Python Scraper.fetch_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scraper.Scraper
的用法示例。
在下文中一共展示了Scraper.fetch_url方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_url
# 需要导入模块: from scraper import Scraper [as 别名]
# 或者: from scraper.Scraper import fetch_url [as 别名]
def process_url(url):
""" Open a URL and process the returned response """
metadata = None
body = None
# Fetch the URL, returning a (metadata, content) tuple or None if error
info = Scraper.fetch_url(url)
if info:
metadata, body = info
if metadata is None:
if Settings.DEBUG:
print("No metadata")
metadata = dict(heading = "",
author = "",
timestamp = datetime.utcnow(),
authority = 0.0)
else:
if Settings.DEBUG:
print("Metadata: heading '{0}'".format(metadata.heading))
print("Metadata: author '{0}'".format(metadata.author))
print("Metadata: timestamp {0}".format(metadata.timestamp))
print("Metadata: authority {0:.2f}".format(metadata.authority))
metadata = vars(metadata) # Convert namedtuple to dict
# Extract the text content of the HTML into a list
tlist = TextList()
extract_text(body, tlist)
text = tlist.result()
# Eliminate soft hyphen and zero-width space characters
text = re.sub('\u00AD|\u200B', '', text)
# Eliminate consecutive whitespace
text = re.sub(r'\s+', ' ', text)
# Tokenize the resulting text, returning a generator
# noinspection PyRedundantParentheses
return (metadata, tokenize(text))