acwing查看做題總數

ChinaUnix博客 2021-09-19 04:23:24 阅读数:638

acwing 查看 看做

import re

import requests

from lxml import etree

class Spider(object):

    def __init__(self):

        self.base_url = '{}/'

        self.headers = {

            'Cookie': 'csrftoken=mixU7wxaV35yyyCDhqbXcIoW3z3Ms0NH31jbbqH; sessionid='

                      '344bo4nowvp9misa9suynjiwz2i5jcof; file_2922585_readed=""; file_2302034_readed=""',

            'Referer': '',

            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'

                          ' (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'

        }

        self.count = 0

    def get_html(self, url):

        html = requests.get(

            url=url,

            headers=self.headers

        ).text

        return html

    def xpath_func(self, html):

        name_bds = '//tbody/tr[./td/span[@title="已通過這道題目"]]/td/a/text()'

        base_obj = etree.HTML(html)

        name_lists = base_obj.xpath(name_bds)

        L = []

        for i in name_lists:

            L.append(i.strip())

        return L

    def re_func(self, html, re_bds):

        pattern = re.compile(re_bds, re.S)

        re_list = pattern.findall(html)

        return re_list

    def parse_html(self, url):

        html = self.get_html(url)

        L = self.xpath_func(html)

        return L

    def run(self):

        warning = input('您馬上就要爬取acwing了,看一下你的做題數,您的勞動成果將會在下面展示出來,外匯跟單gendan5.com確定要看嗎?(Y/N)')

        if warning == 'Y':

            print('爬蟲系統已經啟動...正在努力抓取,請稍等....')

            print('+---------------------------------+')

            print('|            name                 |')

            print('+---------------------------------+')

            for i in range(1, 80):

                url = self.base_url.format(i)

                L = self.parse_html(url)

                for _ in L:

                    self.count += 1

                    print('|   ' + _)

            print('+---------------------------------+')

            print('經過您的不懈努力,您一共做了' + str(self.count) + '道題,繼續努力!!')

        else:

            print('已經退出,你這個弱者')

if __name__ == '__main__':

    spider = Spider()

    spider.run()

版权声明:本文为[ChinaUnix博客]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/09/20210919042324467f.html