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


Python Request.decode方法代码示例

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


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

示例1: extract_bitinfocharts

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import decode [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

示例2: main

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import decode [as 别名]
def main():
    try:
        sys.argv[5]
    except IndexError:
        srv = 2181
    else:
        srv = sys.argv[5]

    if sys.argv[1] == "normal":
        pingms = sys.argv[4]
        dlkbps = sys.argv[2]
        ulkbps = sys.argv[3]
    elif sys.argv[1] == "smart":
        dlkbps = floor(
            float(sys.argv[2]) * float(randrange(9450000, 9750000)) / 10000000)
        ulkbps = floor(
            float(sys.argv[3]) * float(randrange(9450000, 9750000)) / 10000000)
        ulkbps = floor(ulkbps * float(randrange(9350, 9650)) / 10000)
        if int(sys.argv[4]) >= 4:
            pingms = int(sys.argv[4]) + randrange(-3, 10)
        else:
            pingms = sys.argv[4]
    else:
        print("This is madness!")
        sys.exit()

    rawRequest = [
        'download=%s' % int(dlkbps),
        'ping=%s' % pingms,
        'upload=%s' % int(ulkbps),
        'promo=',
        'startmode=%s' % 'pingselect',
        'recommendedserverid=%s' % srv,
        'accuracy=%s' % 1,
        'serverid=%s' % srv,
        'hash=%s' % md5(('%s-%s-%s-%s' %
                         (pingms, int(ulkbps), int(dlkbps), '297aae72'))
                        .encode()).hexdigest()]

    req = Request('http://www.speedtest.net/api/api.php',
                  data='&'.join(rawRequest).encode())
    req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
    x = urlopen(req)
    req = x.read()
    code = x.code
    x.close()

    reqresult = parse_qs(req.decode())
    results = reqresult.get('resultid')

    print('Image: http://www.speedtest.net/result/%s.png' %
          results[0])
    print('In a webpage: http://www.speedtest.net/my-result/%s' % results[0])
开发者ID:Vdragon,项目名称:srg,代码行数:55,代码来源:srg.py

示例3: open

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import decode [as 别名]
for i in ['旅游','出境游','国内游','周边游','旅行']:
    try:
        with open(i+'360.txt') as f:
            with open(i+'.txt', 'a') as g:
                for j in f:
                    j = j.strip()
                    if j in app:
                        print({'keyword':i, 'name':j, 'detail':app[j]}, file=g)
                    else:
                        try:
                            print(j)
                            data = {'kw': j}
                            data = baseUrl + urlencode(data)
                            data = Request(url=data, headers=genHeader())
                            data = urlopen(data).read()
                            sleep(3)
                            data = data.decode()
                            data = loads(data)
                            for k in data['obj']['items']:
                                if j in k['appDetail']['appName'] or k['appDetail']['appName'] in j:
                                    print(k['appDetail']['appName'])
                                    print({'keyword':i, 'name':j, 'detail':k['appDetail']}, file=g)
                                    app[j] = k['appDetail']
                                    break
                        except Exception as e:
                            print('inner', str(e))
                            continue
    except Exception as e:
        print('outer', str(e))
        continue
开发者ID:JinkeCao,项目名称:MMA-Category-Info,代码行数:32,代码来源:app_360_qq.py


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