Recopilar datos para capacitarse en la resolución de problemas de PNL

Seleccionar una fuente y herramientas de implementación





Como fuente de información, decidí usar  habr.com  , un blog colectivo con elementos de un sitio de noticias (se publican noticias, artículos analíticos, artículos sobre tecnología de la información, negocios, Internet, etc.). En este recurso, todos los materiales se dividen en categorías (centros), de los cuales solo los principales: 416 piezas. Cada material puede pertenecer a una o más categorías.





() python. – Jupyter notebook Google Colab. :





  • BeautifulSoup – html / xml;





  • Requests – http ;





  • Re – ;





  • Pandas – .





tqdm ratelim ( ).









, . :





mainUrl = 'https://habr.com/ru/post/'
postCount = 10000
      
      



, , , .  try… except   requests. :





@ratelim.patient(1, 1)
def get_post(postNum):
currPostUrl = mainUrl + str(postNum)
try:
response = requests.get(currPostUrl)
response.raise_for_status()
response_title, response_post, response_numComment, response_rating, response_ratingUp, response_ratingDown, response_bookMark, response_views = executePost(response)
dataList = [postNum, currPostUrl, response_title, response_post, response_numComment, response_rating, response_ratingUp, response_ratingDown, response_bookMark, response_views]
habrParse_df.loc[len(habrParse_df)] = dataList
except requests.exceptions.HTTPError as err:
pass
      
      



– . try – , .





executePost - .





def executePost(page):
soup = bs(page.text, 'html.parser')
#   
title = soup.find('meta', property='og:title')
title = str(title).split('="')[1].split('" ')[0]
#   
post = str(soup.find('div', id="post-content-body"))
post = re.sub('\n', ' ', post)
#   
num_comment = soup.find('span', id='comments_count').text
num_comment = int(re.sub('\n', '', num_comment).strip())
#  -     
info_panel = soup.find('ul', attrs={'class' : 'post-stats post-stats_post js-user_'})
#   
try:
rating = int(info_panel.find('span', attrs={'class' : 'voting-wjt__counter js-score'}).text)
except:
rating = info_panel.find('span', attrs={'class' : 'voting-wjt__counter voting-wjt__counter_positive js-score'})
if rating:
rating = int(re.sub('/+', '', rating.text))
else:
rating = info_panel.find('span', attrs={'class' : 'voting-wjt__counter voting-wjt__counter_negative js-score'}).text
rating = - int(re.sub('–', '', rating))
#         
vote = info_panel.find_all('span')[0].attrs['title']
rating_upVote = int(vote.split(':')[1].split('')[0].strip().split('↑')[1])
rating_downVote = int(vote.split(':')[1].split('')[1].strip().split('↓')[1])
#     
bookmk = int(info_panel.find_all('span')[1].text)
#    
views = info_panel.find_all('span')[3].text
return title, post, num_comment, rating, rating_upVote, rating_downVote, bookmk, views
      
      



BeautifulSoup : soup = bs(page.text, ‘html.parser’).  find / findall  (, html-). , html-, , .





( ), . , 10 . tqdm .





for pc in tqdm(range(postCount)):
postNum = pc + 1
get_post(postNum)
      
      



pandas :





Como resultado, obtuve un conjunto de datos que contiene los textos de los artículos del recurso  habr.com , así como información adicional: el título, el enlace al artículo, la cantidad de comentarios, la calificación, la cantidad de marcadores, la cantidad de visitas. .





En el futuro, el conjunto de datos resultante se puede enriquecer con datos adicionales y usarse para entrenar en la construcción de varios modelos de idiomas, clasificar textos, etc.








All Articles