codeforces crawl-demo

import requests,os,csv,re
from lxml import etree
class codeforces(object):
  global prefix,syspath
  prefix = 'https://codeforces.com'
  syspath = "D:\\szs\\opyion"
  """
  请求主页
  return: 分页链接
  """
  @staticmethod
  def index_page(x):
    content = requests.get("https://codeforces.com/problemset/page/{}?tags=800-1600".format(x)).text
    page_html = etree.HTML(content)
    dark_url = [prefix+i for i in page_html.xpath('//*[@class="problems"]//tr/td[2]/div[1]/a/@href')]
    print("正在解析首页====",dark_url)
    return dark_url
  """
  请求分页
  return: 标题,内容
  """
  @staticmethod
  def page_break(x):
    page_content = requests.get(x).text
    sorter_html = etree.HTML(page_content)
    title = sorter_html.xpath('//*[@class="title"]/text()')[0].split('.')[1]
    print("正在解析子页,===",title)
    statement = sorter_html.xpath('//*[@class="problem-statement"]/div[2]//text()')
    inputo = sorter_html.xpath('//*[@class="input"]//text()')
    output = sorter_html.xpath('//*[@class="output"]//text()')
    return title,statement,inputo,output
  @staticmethod
  def main():
    list_href = list(map(codeforces.index_page,list(range(20,29))))
    we =  [n for a in list_href for n in a ]
    data_list = list(map(codeforces.page_break,we))
    for data_i in data_list:
      zifu = '\/:*?"<>|'
      dirname = re.sub(zifu, "", data_i[0])
      dataPath = os.path.join(syspath, dirname)
      try:
        codeforces.__newdir(dataPath)
      except:
        print(dataPath, "未能新建")
        continue
      try:
        codeforces.__writeTxt(dataPath, ','.join(data_i[1]))
      except:
        print(dataPath, "Txt未能写入")
        continue
      try:
        codeforces.__writeCsv(dataPath, data_i[2])
        codeforces.__writeCsv(dataPath, data_i[3])
      except:
        print(dataPath, "Csv未能写入")
        continue
    return True
  @staticmethod
  def __newdir(path):
    isExists=os.path.exists(path)
    if not isExists:
      os.makedirs(path)
      print(path+'创建成功')
      return
    else:
      return False
  @staticmethod
  def __writeTxt(problem_statement,details):
    f = open(problem_statement+'\\'+'problem_statement.txt','a+',encoding='utf-8')
    f.write(details)
    f.close()
  @staticmethod
  def __writeCsv(testcase,details):
    f = open(testcase+"\\"+'testcase.csv', 'a+')
    writer = csv.writer(f)
    writer.writerow(details)
    f.close()
if __name__ == '__main__':
  zx = codeforces()
  zx.main()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值