当前位置: 首页>>代码示例>>Python>>正文


Python Request.replace方法代码示例

本文整理汇总了Python中urllib.request.Request.replace方法的典型用法代码示例。如果您正苦于以下问题:Python Request.replace方法的具体用法?Python Request.replace怎么用?Python Request.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib.request.Request的用法示例。


在下文中一共展示了Request.replace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: extract_bitinfocharts

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import replace [as 别名]
 def extract_bitinfocharts(self, coin, metric="price", coin_col=False, metric_col=False):
     """Retrieve historical data for a specific cyrptocurrency scraped from bitinfocharts.com
     
     Parameters
     ----------
     coin : the code of the cryptocurrency (e.g. 'btc' for bitcoin)
         full range of available coins can be found on bitinfocharts.com
     metric : the particular coin information to be retrieved
         (options are limited to those listed on bitinfocharts.com
         including 'price', 'marketcap', 'transactions' and 'sentinusd'
     coin_col : whether to include the coin name as a column
         (default is False i.e. the column is not included)
     metric_col : whether to include the metric name as a column
         (default is False i.e. the column is not included)
         
     Returns
     -------
     pandas Dataframe
     """
     if coin not in ['btc', 'eth', 'xrp', 'bch', 'ltc', 'dash', 'xmr', 'btg', 'etc', 'zec', 
                     'doge', 'rdd', 'vtc', 'ppc', 'ftc', 'nmc', 'blk', 'aur', 'nvc', 'qrk', 'nec']:
         raise ValueError("Not a valid coin")
     if metric not in ['transactions', 'size', 'sentbyaddress', 'difficulty', 'hashrate', 'price', 
                       'mining_profitability', 'sentinusd', 'transactionfees', 'median_transaction_fee', 
                     'confirmationtime', 'marketcap', 'transactionvalue', 'mediantransactionvalue',
                      'tweets', 'activeaddresses', 'top100cap']:
         raise ValueError("Not a valid bitinfocharts metric")
     new_col_name = "_".join([coin, metric])
     parsed_page = Request("https://bitinfocharts.com/comparison/{}-{}.html".format(metric, coin),
                         headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'})
     try: 
         parsed_page = urlopen(parsed_page, timeout=self.timeout).read()
         parsed_page = parsed_page.decode("utf8")
     except Exception as e:
         return pd.DataFrame({"error":e}, index=[0])
     start_segment = parsed_page.find("new Dygraph")
     if start_segment != -1:
         start_list = parsed_page.find('[[', start_segment)
         end_list = parsed_page.find(']]', start_list)
         parsed_page = parsed_page[start_list:end_list]
     else:
         return pd.DataFrame({"error":"Could not find the appropriate text tag"}, index=[0])
     parsed_page = parsed_page.replace('new Date(', '')
     parsed_page = parsed_page.replace(')', '')
     parsed_page = parsed_page.replace('null', '0')
     parsed_page = parsed_page.replace('["', '{"date":"')
     parsed_page = parsed_page.replace('",', '","{}":'.format(new_col_name))
     parsed_page = parsed_page.replace('],', '},')
     parsed_page = parsed_page + '}]'
     output = json.loads(parsed_page)
     output = pd.DataFrame(output)
     output['date'] = pd.to_datetime(output['date'], format="%Y-%m-%d")
     output = output[(output['date']>=self.from_date) & (output['date']<=self.to_date)]
     # for consistency, put date column first
     output = output[['date', new_col_name]]
     if coin_col:
         output['coin'] = coin
     if metric_col:
         output['metric'] = metric
     return output.sort_values(by='date', ascending=self.ascending).reset_index(drop=True)
开发者ID:Averroes,项目名称:cryptory,代码行数:62,代码来源:cryptory.py


注:本文中的urllib.request.Request.replace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。