ZeLib0ba

Guns.ru parsing

May 14th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. __author__ = 'ZeLib0ba -> http://surin.ru'
  2. # просто запускаем , на выходе получаем файл links.html
  3. # по умолчанию первые 70 постов поменять можно, см. примечание в коде
  4. import requests
  5. from bs4 import BeautifulSoup
  6. head = """
  7. <!DOCTYPE html>
  8. <html>
  9. <head lang="en">
  10. <link href='http://fonts.googleapis.com/css?family=Ubuntu&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
  11.    <meta charset="UTF-8">
  12.    <style  type="text/css">
  13. body,html {
  14.     font-family: 'Ubuntu', sans-serif;
  15.     color: #000000;
  16. }
  17.  
  18. a:link,a:visited,a:active {color: #000000;text-decoration:none;}
  19. a:hover {text-decoration:underline;}
  20. </style>
  21.  
  22. </head>
  23. <body>
  24. """
  25. def page_url(url):
  26.     page = None
  27.     try:
  28.         r = requests.get(url)
  29.         if r.status_code == 200:
  30.             page = r.text
  31.     except:
  32.         page = 'Error Parsing'
  33.  
  34.     return page
  35. def main():
  36.     all_links = []
  37.     f = open('links.html','w',encoding='utf-8')
  38.     a = page_url('http://forum.guns.ru/forumtopics/9.html')
  39.     soup = BeautifulSoup(a)
  40.     print(soup.text)
  41.     #print(soup.prettify())
  42.     all = soup.find_all('a')
  43.     print(all)
  44.     for i in all:
  45.         print(i)
  46.         forumlink = i.get('href')
  47.  
  48.         #print (dir(forumlink))
  49.         bbb = i.get('target')
  50.  
  51.         if "http://forum.guns.ru/forummessage/9/" in forumlink:
  52.             if "-"  not in forumlink:
  53.                 if not bbb:
  54.                     if not '>1<' in str(i):
  55.                         all_links.append(i)
  56.                 #print(forumlink,len(forumlink),len(str(i)))
  57.     f.write(head)
  58.     count = 0
  59.     for i in all_links:
  60.         if not 'ПРАВИЛА РАЗДЕЛА' in i:
  61.             aaa = str(i).replace('<b>','')
  62.             bbb = str(aaa).replace('</b>','')
  63.             print(bbb)
  64.             links123 = str(count) + '. ' + str(bbb).lower() + "<br>"
  65.             #print(links123)
  66.             f.write(links123)
  67.             count +=1
  68.             if count == 71: # тут поменять кол-ва постов для загрузки
  69.                 break
  70.             print(count)
  71.     f.write('</body></html>')
  72.  
  73. if __name__ == '__main__':
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment