Oleg_Kornilov

Волков LiveJournal posts parsing

Mar 27th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2.  
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import os, re
  6. import sys, traceback  
  7.  
  8.  
  9. URLS = [
  10. ' salery. livejournal. com/2006',
  11. 'salery. livejournal. com/2007' до 2017 г.
  12. ]
  13.  
  14. #тхт файлы
  15. pre = 'C:\\Users\\...\\Desktop\\pre.txt'
  16. post = 'C:\\Users\\...\\Desktop\\post.txt'
  17. links = 'C:\\Users\\...\\Desktop\\links.txt'
  18. linksAndTitles = 'C:\\Users\\...\\Desktop\\linksAndT.txt'
  19. end = 'C:\\Users\\...\\Desktop\\end.txt'
  20.  
  21. #получаем линки постов жж по месяцам
  22. # salery. livejournal. com/2006/11/ - первая запись в жж была 11 ноября 2006
  23. # salery.livejournal. com /2006/12/
  24. # и т.д.
  25. def preLinks():
  26.     f = open(pre, 'w')
  27.     try:
  28.         for link in URLS:
  29.             r = requests.get(link, 'html.parser')
  30.             soup = BeautifulSoup(r.content, 'html.parser')
  31.             for link in soup.find_all('td', class_='caption', align='right'):
  32.                 link2 = link.a.get('href')          
  33.                 f.write(link2 + '\n')
  34.                 print(link2)
  35.     except Exception:
  36.         print(traceback.format_exception(*sys.exc_info())[1])
  37.         input()
  38.    
  39.  
  40.  
  41.  
  42. def postLinks():
  43.     f = open(pre, 'r')
  44.     f1 = open(post, 'w')    
  45.     for i in f:
  46.         print(i)
  47.         r = requests.get(i, 'html.parser')
  48.         soup = BeautifulSoup(r.content, 'html.parser')
  49.         for link in soup.find_all('dt'):
  50.             link2 = link.a.get('href')          
  51.             print(link2)    
  52.             f1.write(link2 + '\n')
  53.     f.close()
  54.     f1.close()                
  55.  
  56.  
  57.  
  58.  
  59. def linkz():
  60.    f = open(post, 'r')
  61.    f1 = open(links, 'w')    
  62.    for i in f:
  63.        print(i)
  64.        r = requests.get(i, 'html.parser')
  65.        soup = BeautifulSoup(r.content, 'html.parser')
  66.        for link in soup.find_all('dt'):
  67.            link2 = link.a.get('href')          
  68.            #linkPure = link2.get('href')
  69.            print(link2)    
  70.            f1.write(link2 + '\n')
  71.  
  72.  
  73.  
  74.  
  75. f1 = open(end, 'w') ???
  76. f = open(post, 'r')  ???
  77.  
  78.          
  79. def main():
  80.     f1 = open(end, 'w') ????
  81.     f = open(post, 'r') ???
  82.     try:
  83.         for i in f:
  84.             r = requests.get(i, 'html.parser')
  85.             soup = BeautifulSoup(r.content, 'html.parser')
  86.            # post = soup.find('td', colspan='2').text
  87.            # print(post)
  88.         #получаем дату поста, записываем ее
  89.             title = soup.title.text
  90.             print(title)
  91.             f1.write(title + '\n')
  92.        
  93.         #получаем название поста
  94.             for i in soup.find_all('td', class_="caption post-title"):
  95.                 try:
  96.                     name = (i.text)
  97.                     f1.write(name + '\n')
  98.                     print(name)
  99.                 except ValueError:
  100.                     print('No name')
  101.                     print(traceback.format_exception(*sys.exc_info())[1])
  102.                     continue
  103.                              
  104.       #получаем линк поста
  105.             for i in soup.find_all('td', align="left", class_="comments", valign="top"):
  106.                 link = (i.a.get('href') + '\n')
  107.                 f1.write(link)
  108.                 f1.write('\n')
  109.                 print(link, '\n')
  110.    
  111.     except Exception:
  112.         print(traceback.format_exception(*sys.exc_info())[1])
  113.         input()
  114.        
  115.     f1.close()
  116.  
  117.  
  118.  
  119.             #ПОЛУЧАЕМ ТЕКСТ ПОСТА
  120. def text():            
  121.     f1 = open(end, 'w')
  122.     f = open('C:\\Users\\....\\Desktop\\Volkov\\end.txt', 'r')
  123.     for i in f:
  124.         f1.write(i)
  125.         match = re.match('http', i)
  126.        
  127.         if match:
  128.             print(i)
  129.             enter = '\n'
  130.             r = requests.get(i, 'html.parser')
  131.             soup = BeautifulSoup(r.content, 'html.parser')
  132.             t = (soup.find('td', colspan='2').text)
  133.          
  134.             try:
  135.                 print(t)
  136.  
  137.             except UnicodeError as err:
  138.                 print("Error: {0}".format(err))            
  139.                 continue
  140.            
  141.             f1.write(t + '\n\n\n\n\n')
  142.     input()
  143.                
  144.        
  145. text()    
  146.  
  147. #f1.close()
Add Comment
Please, Sign In to add comment