利用urllib从ks5u下载2016高考真题

# coding: utf-8
import urllib.request
import http.cookiejar
import logging,os
from lxml import etree

#配置日志级别为INFO
logging.basicConfig(level=logging.INFO)

#启用cookie自动管理
cj=http.cookiejar.CookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)

#post登陆请求参数
values={'userid':'******','userpass':'******'}
data = urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes
ks5u_login_url='http://www.ks5u.com//User/Chk_UserLogin.asp'
req = urllib.request.Request(ks5u_login_url, data)

#登陆网站,获得授权
logging.info('开始登陆网站!')
with urllib.request.urlopen(req) as response:
    html=response.read()
    # with open('ks5u_login.html','w') as outfile:
        # outfile.write(html.decode('gb2312'))
logging.info('登陆成功!')

logging.info('访问2016高考真题下载页面!')
all_url='http://old.ks5u.com/shiti/gaokao/2016/'
with urllib.request.urlopen(all_url) as response:
    html=response.read().decode('utf-8')
    # with open('ks5u.html','w',encoding='utf-8') as outfile:
        # outfile.write(html)
        # logging.info('写入文件ks5u.html成功!')
    root=etree.HTML(html)  #利用lxml解析获得的html文件
    #利用xpath搜索节点
    #'//div[@class="sub_jiexi"]'
    #//表示任意节点
    #div节点名称
    #[@...]属性满足的条件
    for div in root.xpath('//div[@class="sub_jiexi"]'):
        for a in div:
            if a.text=='解析':
                # headers中不能包含中文字符,否则'latin-1'不能编码
                # 某些站点有所谓的反盗链设置,其实说穿了很简单,
                # 就是检查你发送请求的header里面,referer站点是不是他自己,
                # 所以我们只需要像把headers的referer改成该网站即可
                headers = {
                    'Referer':a.get('href')
                }
                fileid=a.get('href').split('/')[-1].split('.')[0]
                doc_url='http://www.ks5u.com/USER/INC/Downsch.asp?id='+fileid
                req = urllib.request.Request(doc_url, None, headers) 
                logging.info('开始下载编号为%s的文件'%(fileid,))
                if not fileid or not fileid.isdigit():
                    continue
                with urllib.request.urlopen(req) as r2:
                    # 取得doc文件名时编码的转换由ISO-8859-1转为gbk
                    filename=r2.headers.get_filename().encode('ISO-8859-1').decode('gbk')
                    #如果文件存在,跳过
                    if os.path.exists(filename):
                        logging.info('文件%s已经存在!'%filename)
                        continue
                    logging.info('获得文件名%s'%filename)
                    #下载文件
                    with open(filename,'wb') as outfile:
                        try:
                            outfile.write(r2.read())
                            logging.info('保存文件%s成功!'%(filename,))
                        except :
                            pass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值