从https://github.com/reddit/reddit/wiki/API:
许多默认用户代理(例如“ Python / urllib”或“ Java”)都受到严格限制,以鼓励使用唯一且具有描述性的用户代理字符串。
这也适用于常规请求。发出请求时,您需要提供自己的用户代理标头。
#TODO: change user agent stringhdr = { 'User-Agent' : 'super happy flair bot by /u/spladug' }req = urllib2.Request(url, headers=hdr)html = urllib2.urlopen(req).read()但是,这将为每个请求创建一个新的连接。我建议用另一种库,能够重新使用连接的
httplib或请求,例如。它将减轻服务器的压力并加快请求的速度:
import httplibimport timelst = """sciencescifi"""hdr= { 'User-Agent' : 'super happy flair bot by /u/spladug' }conn = httplib.HTTPConnection('www.reddit.com')for name in lst.split(): conn.request('GET', '/r/'+name, headers=hdr) print conn.getresponse().read() time.sleep(2)conn.close()


