抓取 题目描述 生成 解题报告 模板 python

来源:互联网 发布:网络电影制作合同 编辑:程序博客网 时间:2024/05/27 19:26

BGround


偷懒是人类进步的阶梯!!
瞎搞是自然选择的必然!!
这绝对是我写过最丑的python代码,但是的确能用,忍忍看吧

Code


# -*- coding: utf-8 -*-import requests, time, reimport HTMLParserdef getPage(html, url, headers, params = {}, timeout = 5, verify = True):    response = html.get(url = url, headers = headers, params = params, timeout = timeout, verify = verify)    page = response.content    return pagedef search(string, page, flags = 0):    pattern = re.compile(string, flags = flags)    results = re.findall(pattern, page)    return resultsdef format(string):    results = HTMLParser.HTMLParser().unescape(re.compile(r'<[^>]+>', re.S).sub('', re.sub('</strong>|</h2>', '\n---\n', re.sub('<strong>|<h2>', '\n##', string.decode('UTF-8')))))    return resultsdef main():    html = requests.session()    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0'}    probid = raw_input('id: ')    url = 'https://www.luogu.org/problem/show?pid=%s#sub' % probid    PD = search(r"<div class=\"lg-article am-g\">(.+?)<div class=\'lg-article-sub am-g\' id=\"sub\"></div>", getPage(html, url, headers), flags = re.S)[0]    with open('/home/olahiuj/文档/progs/oi/luogu%s.cpp' % probid, 'r') as file:        content = '''        %s## Analysis---## Code---` ``%s` ``        ''' % (re.sub('    ', '', format(PD).encode('UTF-8')), file.read().encode('UTF-8'))    with open('/home/olahiuj/文档/LPD/luogu%s.md' % probid, 'w') as file:        file.write(content)    print 'Problem %s is Done!' % probidif __name__ == '__main__':    main()
0 0
原创粉丝点击