在python2.x中,dict.keys()返回一个列表,在python3.x中,dict.keys()返回一个dict_keys对象,比起列表,这个对象的行为更像是set,所以不支持索引的
useful_proxies.keys()={'117.90.252.55:9000': 0, '119.29.103.13:8888': 0, '175.155.227.130:8118': 0, '123.171.55.45:808': 0, '117.69.230.18:27853': 0, '125.83.193.152:8998': 0, '222.208.83.175:9000': 0, '223.145.230.146:6666': 0, '117.68.241.52:808': 0, '114.239.127.225:61234': 0}
proxy = random.choice(useful_proxies.keys())
在python2.7下执行正确
在python3.6中 TypeError: 'dict_keys' object does not support indexing
解决办法
proxy = random.choice(list(useful_proxies))