我目前正在尝试使用Python登录网站,但该网站似乎在同一页面上发送cookie和重定向语句.Python似乎遵循该重定向,从而阻止我阅读登录页面发送的cookie.如何防止Python的urllib(或urllib2)urlopen跟随重定向?
我有以下python脚本,它工作得很漂亮.
import urllib2
url = 'http://abc.com' # write the url here
usock = urllib2.urlopen(url)
data = usock.read()
usock.close()
print data
Run Code Online (Sandbox Code Playgroud)
但是,我给它的一些URL可能会重定向它2次或更多次.在加载数据之前,如何让python等待重定向完成.例如,使用上面的代码时
http://www.google.com/search?hl=en&q=KEYWORD&btnI=1
Run Code Online (Sandbox Code Playgroud)
这是在谷歌搜索上点击我的幸运按钮的等价物,我得到:
>>> url = 'http://www.google.com/search?hl=en&q=KEYWORD&btnI=1'
>>> usick = urllib2.urlopen(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error …Run Code Online (Sandbox Code Playgroud) 我想知道如何阻止urllib2跟随我选择的网址上的重定向请求.我在浏览时发现了这段代码,但它似乎全局工作,我只希望它禁用某个网址上的重定向:
import urllib2
class RedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
result.status = code
return result
http_error_301 = http_error_303 = http_error_307 = http_error_302
opener = urllib2.build_opener(RedirectHandler())
webpage = opener.open('http://www.website.com').geturl()
print webpage
Run Code Online (Sandbox Code Playgroud)
我还应该提一下,我正在使用urllib.urlopen('site.com')请求网址,我希望第一次重定向允许发生,例如说site.com重定向到site.com/redirect但是它尝试再次从site.com/redirect重定向到site.com/secondredirect我希望脚本能够识别网址中的"secondredirect"并停止发生该请求.我希望我能很好地解释这一切并希望看到一些回复,因为我花了几个小时试图解决这个问题:头痛:
我正在尝试获取包括3XX在内的http状态代码,但是无法从我的代码中打印出来。
这是代码:
import urllib
import urllib.request
import urllib.error
urls = ['http://hotdot.pro/en/404/', 'http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org', 'http://www.voidspace.org.uk']
fh = open("example.txt", "a")
def getUrl(urls):
for url in urls:
try:
with urllib.request.urlopen(url) as response:
requrl = url
the_page = response.code
fh.write("%d, %s\n" % (int(the_page), str(requrl)))
except (urllib.error.HTTPError, urllib.error.URLError) as e:
requrl = url
print (e.code)
fh.write("%d, %s\n" % (int(e.code), str(requrl)))
getUrl(urls)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄这个吗?
我有这个简单的代码:
import requests
r = requests.get('https://yahoo.com')
print(r.url)
Run Code Online (Sandbox Code Playgroud)
执行后打印:
https://uk.yahoo.com/?p=us
Run Code Online (Sandbox Code Playgroud)
我想看看:
在到达之前发生了多少次重定向https://uk.yahoo.com/?p=us(显然,我最初输入时有重定向https://yahoo.com)?
我还想保存每一页的内容,而不仅仅是最后一页。这个怎么做?
python web-scraping python-3.x python-requests python-requests-html