37,719
社区成员
发帖
与我相关
我的任务
分享
# -*- coding: utf-8 -*-
import requests
import urllib2
import cookielib
#声明一个CookieJar对象实例来保存cookie
cookie = cookielib.CookieJar()
#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
handler=urllib2.HTTPCookieProcessor(cookie)
#通过handler来构建opener
opener = urllib2.build_opener(handler)
#此处的open方法同urllib2的urlopen方法,也可以传入request
response = opener.open('https://asos.tmall.com/search.htm')
#?spm=a1z10.5-b.w4011-5044691060.102.QqiXRs')
for item in cookie:
print 'Name = '+item.name
print 'Value = '+item.value
Traceback (most recent call last):
File "D:/python/testgethtml.py", line 13, in <module>
response = opener.open('https://asos.tmall.com/search.htm')
File "D:\python\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "D:\python\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python\lib\urllib2.py", line 442, in error
result = self._call_chain(*args)
File "D:\python\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "D:\python\lib\urllib2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "D:\python\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "D:\python\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python\lib\urllib2.py", line 442, in error
result = self._call_chain(*args)
File "D:\python\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "D:\python\lib\urllib2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "D:\python\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "D:\python\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python\lib\urllib2.py", line 442, in error
result = self._call_chain(*args)
File "D:\python\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "D:\python\lib\urllib2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "D:\python\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "D:\python\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python\lib\urllib2.py", line 442, in error
result = self._call_chain(*args)
File "D:\python\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "D:\python\lib\urllib2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "D:\python\lib\urllib2.py", line 404, in open
response = self._open(req, data)
File "D:\python\lib\urllib2.py", line 422, in _open
'_open', req)
File "D:\python\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "D:\python\lib\urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "D:\python\lib\urllib2.py", line 1184, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 1] _ssl.c:504: error:140943FC:SSL routines:SSL3_READ_BYTES:sslv3 alert bad record mac>
# -*- coding: utf-8 -*-
import requests
import urllib2
import cookielib
import time
#声明一个CookieJar对象实例来保存cookie
cookie = cookielib.CookieJar()
#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
handler=urllib2.HTTPCookieProcessor(cookie)
#通过handler来构建opener
opener = urllib2.build_opener(handler)
#此处的open方法同urllib2的urlopen方法,也可以传入request
urllib2.install_opener(opener)
for _ in range(20):
try:
response = opener.open('https://asos.tmall.com/search.htm').read()
time.sleep(1)
except urllib2.URLError,e:
print e.reason
print('='*40)
import time
import urllib
from http.cookiejar import CookieJar
#声明一个CookieJar对象实例来保存cookie
cookie = CookieJar()
#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
handler=urllib.request.HTTPCookieProcessor(cookie)
#通过handler来构建opener
opener = urllib.request.build_opener(handler)
#此处的open方法同urllib2的urlopen方法,也可以传入request
urllib.request.install_opener(opener)
for _ in range(10):
response = opener.open('https://asos.tmall.com/search.htm').read()
#?spm=a1z10.5-b.w4011-5044691060.102.QqiXRs')
for item in cookie:
print('Name = %s' % item.name)
print('Value = %s' % item.value)
time.sleep(1)
print('='*40)
不过爬这个网站没这么简单,有很多ajax的请求,继续努力!