フォームNo.16

ロシアの大学で働く教師は、彼らの科学的および教育的研究のリストを行政に提供する必要性に定期的に直面しています。たとえば、役職への(再)選挙、称号の授与など。情報を提示するための形式であるフォームNo. 16は、科学高等省の官僚的な深みでいつ使用されているかを知るために開発されました。ロシア連邦の教育。このばかげたフォームに手動で入力するのが面倒になり、科学電子図書館elibrary.ruから取得した情報に基づいて必要なテーブルを生成する小さなPythonスクリプトを作成しました。おそらく誰かがこれに興味を持っているので、以下はこの手順の説明です...





, elibrary.ru, , «» « ». «», « ». , html-, index.html



. :





elibrary.ruのテーブル行
elibrary.ru

№268 ( №3 . 52) - :





フォームNo.16に従ったテーブル行
№16

テーブル形式を変換するためのスクリプトは、BeautifulSoupライブラリを使用することに基づいています。これは、私が非常に大雑把になり、人生で初めて使用したものです。これが私が得たものです:





#!/usr/bin/env python3
from bs4 import BeautifulSoup
from random import randint
from re import findall 

YFrom, YTo = 2015, 2020                              #    

def NP(s): #        
  pages = s.split()[-1]
  if '-' in pages:
    P = pages.split('-')
    np =  1 + int(float(P[1])-float(P[0]))
  else:
    np = randint(5, 10)
  return '%d' % np #    
  
def Year(s, FROM, TO): #      
  Ys = findall(r'\s\d{4}\.', s)                 #    ' 2020.'
  if not Ys: Ys = findall(r'\s\d{4}', s)        #    ' 2020'
  if not Ys: return False        #     -                              
  for y in Ys: Y = int(float(y)) #        
  if Y<FROM or Y>TO: return False 
  else:              return True

with open('index.html', 'r') as fp: 
  soup = BeautifulSoup(fp, 'html.parser')              #   
soup.head.style.decompose()                            #  , css  ..
aname = soup.title.get_text().split('-')[1]            #  
aname = f'        -  {aname:s}\n'
soup.title.string = aname                              #  
soup.find('span').string = aname                       #  
soup.find('i').decompose()                             #  - 
soup.find('table').decompose()                         #     
table = soup.find('table')                             #   
table['border'] = 1                                    #  
table['width']  = '100%'                               #  
N = 1                                                  #   
rows = table.find_all('tr')                            #     
for i in range(len(rows)):                             #     
  cols = rows[i].find_all('td')                        #   
  if len(cols)==3 and cols[1].find('span'):            #     
    content = cols[1].get_text()                       #     
    title   = cols[1].find('span').get_text()          #  
    authors = cols[1].find('i').get_text()             #  
    cites   = int(cols[2].get_text())                  #   
    content = content.replace(title, '')               #  ,   :
    content = content.replace(authors, '')             #  content    
    thesis  = content.replace(' : ','')       #     
    abbook  = content.replace(' : ','')          # 
    if   thesis != content:                            #
      title += ' ()';      content = thesis      #
    elif abbook != content:                            #
      title += ' ()';      content = abbook      #
    else:                                              #
      if ''  in content: title+= ' ()'#
      elif '' in content: title+= ' ()'#
      else: title += ' ()'                       #
    authors = authors.split(', ')                      #   
    if cites<10 or not Year(content, YFrom, YTo):      #    
      rows[i].decompose()                              #
    else:                                              #   -  -  
      anumber = len(authors)
      if anumber<5: PS = ''
      else:         PS = f'  .,  {anumber:d} .'
      authors = ', '.join(authors[0:5]) + PS

      cols[0].string = f'{N:3d}'                        #  
      cols[1].string = title                            # 
      cols[2].string = "."                           #  
      for info in [content, NP(content), authors]:      #    
        A = soup.new_tag('td');  A.string = info ; rows[i].append(A)
      N+= 1
  else:
    rows[i].decompose()

tr = soup.new_tag('tr') #   
names = ['№ \', ' ,  ', ' ', ' ', '  ..  .', '']
for name in names:
  th = soup.new_tag('th') 
  th.string = name
  tr.append(th) 
table.insert(0, tr)
  
with open('table.html', 'w', encoding='utf-8') as fp: fp.write(str(soup))    
      
      



タスクを完了するにはindex.html



、elibrary.ruを使用してテーブルを保存したファイルを含むフォルダーでスクリプトを実行する必要があります出力では、table.html



Googleドキュメントに簡単にアップロードできるファイルが生成され、列幅の変更、フォントの選択などの最終編集を行うことができます。








All Articles