好的,我亲爱的帮手,这是问题,我无法获得“ http://example.com/#sharplink ”,顺便说一下,在站点中进行无限循环,所以我使用了重定向处理程序,它需要启用 cookielibrary,
这是我的代码
import urllib2, urllib, cookielib
urllib.FancyURLopener.version = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.3) Gecko/2008092814 (Debian-3.0.1-1)'
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def redirect_request(self, req, fb, code, msg, headers, newurl):
m = req.get_method()
if (code in (301, 302, 303, 307) and m in ('GET', 'HEAD') or code in (301, 302, 303) and m == 'POST'):
newurl = newurl.replace(' ', '%20')
newheaders = dict((k,v) for k,v in req.headers.items()
if k.lower() not in ("content-length", "content-type")
)
return urllib2.Request(newurl,
headers=newheaders,
origin_req_host=req.get_origin_req_host(),
unverifiable=True)
else:
raise HTTPError(req.get_full_url(), code, msg, headers, fp)
cj = cookielib.CookieJar()
opener = urllib2.build_opener(MyHTTPRedirectHandler, urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
req = urllib2.Request('http://example.com/goto/#sharplink')
response = urllib2.urlopen(req)
f=open('bet','w')
f.write(response.read())
f.close()
但每次我只能得到' http://example.com/goto '页面而不是锐利的页面,请帮助我!!!