zoukankan      html  css  js  c++  java
  • The bytes/str dichotomy in Python 3

    The bytes/str dichotomy in Python 3 - Eli Bendersky's website https://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3

     Arguably the most significant new feature of Python 3 is a much cleaner separation between text and binary data. Text is always Unicode and is represented by the str type, and binary data is represented by thebytes type. What makes the separation particularly clean is that str and bytes can't be mixed in Python 3 in any implicit way. You can't concatenate them, look for one inside another, and generally pass one to a function that expects the other. This is a good thing.

    import requests
    import re
    import time
    from redis import Redis

    REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
    rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD)

    f, url_l, filter_replace_l = 'DISTINCT_url.txt', [], [' ', ' ', ' ']
    with open(f, 'r', encoding='utf-8') as fr:
    for i in fr:
    try:
    for ii in filter_replace_l:
    i = i.replace(ii, '')
    rds.sadd('chk_url_all', i)
    except Exception as e:
    print(e)

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
    while True:
    try:
    url = rds.spop('chk_url_all').decode('utf-8')
    s = 'http://'
    if s not in url:
    url = '{}{}'.format(s, url)
    print(url)
    r = requests.get(url, headers=headers, timeout=50)
    print(r.status_code)
    except Exception as e:
    print(e)



    从redis取值结果为bytes type



  • 相关阅读:
    找到搜索的北了
    zoj2741Offside
    hdu: 1426 ( Sudoku Killer )
    悲剧的矩阵乘法
    UVa729 The Hamming Distance Problem
    hdu 1584 蜘蛛牌
    zoj2972Hurdles of 110m
    hdu 1372 ( Knight Moves ) BFS
    UVa193Graph Coloring
    zjut1624序列问题
  • 原文地址:https://www.cnblogs.com/rsapaper/p/8478011.html
Copyright © 2011-2022 走看看