python3.x - Python3 下载rar文件 打开后出错
高洛峰
高洛峰 2017-04-17 15:25:22
0
2
792

点击某个链接后就可以下载文件
代码如下:

import requests
import os
import urllib.request

url = 'http://www.hacker.com.cn/index.php?m=content&c=down&a=download&a_k=bd8dBFECVFYCUlUBAQJbAw5VBF0MAgJVUQIICAkPBQoGBQASBQ9VQEFfQ0UMVQwCDlVRDwEDABFZQw0NAEgKCB8EBxpQBF1AX19UF1dZUEVMFFsXF0VHQB5bUVtTA0oWUl5aGgJcSxNCDgpQVQJRXV1LUwgJBx8HBQEFFwpWCQ0BBAUBUANRVQdaVwUBSkpQSkIMV1xXXF5UDgI'
req = urllib.request.Request(url)
data = urllib.request.urlopen(req)
urllib.request.urlretrieve(url,"test.rar")

下载后打开提示如下:


于是我继续用了Requests库去下载

import requests
import os
import urllib.request

url = 'http://www.hacker.com.cn/index.php?m=content&c=down&a=download&a_k=bd8dBFECVFYCUlUBAQJbAw5VBF0MAgJVUQIICAkPBQoGBQASBQ9VQEFfQ0UMVQwCDlVRDwEDABFZQw0NAEgKCB8EBxpQBF1AX19UF1dZUEVMFFsXF0VHQB5bUVtTA0oWUl5aGgJcSxNCDgpQVQJRXV1LUwgJBx8HBQEFFwpWCQ0BBAUBUANRVQdaVwUBSkpQSkIMV1xXXF5UDgI'
f = requests.get(url)
with open("code.rar","wb") as code:
    code.write(f.content)

结果还是一样的

总结:无论是urlretrieve 还是直接open文件写入都是不对,,求解。。。
----------------------------问题已解决--------------------------------
问题反馈:
问题已解决如下:加入cookie访问下载链接,可以成功下载,附代码

import requests
import os
import re

#黑客防线杂志——杂志下载页面
info_url='http://www.hacker.com.cn/show-7-2747-1.html'

#加入headers
heads={
        'Referer': 'http://www.hacker.com.cn/show-7-2747-1.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSER'
        }
#保持cookie
s = requests.Session()
data = s.get(info_url)
data = data.text

#正则提取下载地址
pattern = re.compile('

<h5 class="tit">下载地址</h5>

.*?<li><a href=\'(.*?)\' target.*?',re.S)
download_page = re.findall(pattern,data)
download_page = download_page[0]
r= s.get(download_page)
r = r.text

#进入下载地址后,再次正则提取下载链接
pattern_down = re.compile('<a href="(.*?)" class="xzs_btn"></a>',re.S)
download_url = re.findall(pattern_down,r)
download_url = download_url[0]

download_url = 'http://www.hacker.com.cn/index.php'+download_url

file = s.get(download_url)

#二进制形式写入文件
with open('mybook.rar','wb') as code:
    code.write(file.content)
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
迷茫

Open the URL you downloaded directly with a browser and find that it is not a rar file. What is returned is a web page

If this is indeed a rar file, you may need to log in and add COOKIE to the request to download

黄舟

The browser opening parameter is wrong. At this time, if you open it with a text editor, you will find that it is an HTML file

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template