爬取Python百例代码

import requests
import chardet
import re
import logging
logging.basicConfig(level=logging.WARNING,format='%(asctime)s-%(levelname)s:%(message)s')

def get_timu():
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
              'Host':'www.runoob.com'}
    for i in range(100):
        link = 'https://www.runoob.com/python/python-exercise-example{}.html'.format(i+1)
        r = requests.get(link,headers=headers,timeout=10)

        #  解决中文乱码P149《python网络爬虫从入门到实践》
        after_gzip = r.content
        after_gzip = after_gzip.decode('utf-8')
        timu = re.search('题目:</strong>([\w\W]*?)</p>',after_gzip)
        try:
            timu = timu.group(1)
        except Exception as e:
            print(e)
            logging.warning('timu {}'.format(i+1))
            timu = 'dss'        
        
        try:
            fenxi = re.search('程序分析:</strong>([\w\W]*?)<div class="example">',after_gzip)
            fenxi = fenxi.group(1)
            
        except:
            try:
                fenxi = re.search('程序分析:</strong>([\w\W]*?)程序源代码:',after_gzip)
                fenxi = fenxi.group(1)
            except:
                fenxi = 'syy'
                logging.warning('fenxi {}'.format(i+1))
            
        fenxi = fenxi.replace('程序源代码:',' ')
        fenxi = fenxi.replace('</p>',' ')
        fenxi = fenxi.replace('<p>',' ')    


        with open(r"百例题目.txt","a",encoding='utf-8') as f:
            f.write('实例{}\n'.format(i+1))
            f.write('题目: '+timu+'\n')
            f.write('分析: '+fenxi+'\n\n')

with open(r"百例题目.txt","w",encoding='utf-8') as f:
    f.write('\n')
get_timu()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值