现在,这是不确定的。 Gzip,图像,有时它不起作用。
如何修改此下载功能以便它可以处理任何内容? (无论是gzip还是任何标题?)
如果是gzip,如何自动“检测”?我不想总是传递真/假,就像我现在做的那样。
def download(source_url, g = False, correct_url = True):
try:
socket.setdefaulttimeout(10)
agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
ree.add_header('Accept-encoding', 'gzip')
opener = urllib2.build_opener()
h = opener.open(ree).read()
if g:
compressedstream = StringIO(h)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
return data
else:
return h
except Exception, e:
return ""
答案 0 :(得分:4)
检查Content-Encoding
标题:
import urllib2
import socket
import random
import StringIO
import gzip
def download(source_url):
try:
socket.setdefaulttimeout(10)
agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
ree.add_header('Accept-encoding', 'gzip')
opener = urllib2.build_opener()
response = opener.open(ree)
encoding=response.headers.getheader('Content-Encoding')
content = response.read()
if encoding and 'gzip' in encoding:
compressedstream = StringIO.StringIO(content)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
return data
else:
return content
except urllib2.URLError as e:
return ""
data=download('http://api.stackoverflow.com/1.0/questions/3708418?type=jsontext')
print(data)
如果您正在处理的服务器没有将内容编码报告为gzip,那么您可以通过try
首先进行更积极的攻击:
def download(source_url):
try:
socket.setdefaulttimeout(10)
agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
ree.add_header('Accept-encoding', 'gzip')
opener = urllib2.build_opener()
response = opener.open(ree)
content = response.read()
compressedstream = StringIO.StringIO(content)
gzipper = gzip.GzipFile(fileobj=compressedstream)
try:
data = gzipper.read()
except IOError:
data = content
return data
except urllib2.URLError as e:
return ""
答案 1 :(得分:1)
要检测您下载的数据类型,您应该替换h = opener.open(ree).read()
的{{1}}。
现在你有了响应对象。您可以使用h.headers(类似dict)对象来分析标头。特别是你会对标题'content-type'和'content-encoding'感兴趣。您可以确定分析它的内容。
h = opener.open(ree)
答案 2 :(得分:1)
import urllib2
import StringIO
import gzip
req = urllib2.Request('http:/foo/')
h = urllib2.urlopen(req)
data = resp.read()
if 'gzip' in resp.headers['Content-Encoding']:
compressedstream = StringIO(h)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
# etc...