77"""Скрипт собирает цитаты сайта bash.im"""
88
99
10- def get_logger (name , file = 'log.txt' , encoding = 'utf8' ):
11- import logging
12- import sys
10+ import logging
11+ import sys
12+ import time
13+ import traceback
14+
15+ from datetime import datetime
16+
17+
18+ import requests
19+
20+ import sqlalchemy .exc
21+ from sqlalchemy import create_engine , Table , Column , Integer , String , MetaData , DateTime
22+ from sqlalchemy .orm import mapper , sessionmaker
23+
24+ from bs4 import BeautifulSoup
1325
26+
27+ def get_logger (name , file = 'log.txt' , encoding = 'utf8' ):
1428 log = logging .getLogger (name )
1529 log .setLevel (logging .DEBUG )
1630
@@ -33,18 +47,6 @@ def get_logger(name, file='log.txt', encoding='utf8'):
3347logger = get_logger ('collector_bash_im' )
3448
3549
36- from sqlalchemy import create_engine , Table , Column , Integer , String , MetaData , DateTime
37- from sqlalchemy .orm import mapper , sessionmaker
38- import sqlalchemy .exc
39-
40- import time
41- import traceback
42-
43- import grab
44-
45- from datetime import datetime
46-
47-
4850class Quote :
4951 def __init__ (self , id , date , rating , text ):
5052 self .id = id
@@ -92,19 +94,8 @@ def get_session_factory():
9294
9395
9496if __name__ == '__main__' :
95- # http://bash.im
96-
97- # g = grab.Grab()
98- # g.go('http://bash.im')
99- #
100- # print(g.response.body_as_bytes())
101-
102- import requests
10397 rs = requests .get ('http://bash.im' )
104- # print(rs.text)
105-
106- from bs4 import BeautifulSoup
107- soup = BeautifulSoup (rs .text , "lxml" )
98+ soup = BeautifulSoup (rs .content , "lxml" )
10899 # print(soup)
109100 i = 0
110101 for quote in soup .find_all (attrs = {"class" : "quote" }):
@@ -115,27 +106,26 @@ def get_session_factory():
115106 i += 1
116107 # print(quote.find(attrs={"class": "text"}).contents)
117108 print (i , text )
118- # quit()
119-
120- quit ()
121109
122- xpath = '//*[@class="current"]/*[@class="page"]/@value'
123- current_page = int (g .doc .select (xpath ).text ())
124- print (current_page )
110+ sys .exit ()
125111
126- xpath = '//*[@class="quote"]'
127- i = 0
128- for quote in g .doc .select (xpath ):
129- print (quote .text ())
130- nodes = quote .node .find_class ('id' )
131- if nodes :
132- i += 1
133- quote_id = int (nodes [0 ].text .replace ('#' , '' ))
134- date = datetime .strptime (quote .node .find_class ('date' )[0 ].text , '%Y-%m-%d %H:%M' )
135- rating = int (quote .node .find_class ('rating' )[0 ].text )
136- text = quote .node .find_class ('text' )[0 ]
137- print (dir (text ))
138- print (text .text_content ())
139- quit ()
140- # print(i, Column(quote_id, date, rating, text))
141- print (i , quote_id , date , rating , text )
112+ # xpath = '//*[@class="current"]/*[@class="page"]/@value'
113+ # current_page = int(g.doc.select(xpath).text())
114+ # print(current_page)
115+ #
116+ # xpath = '//*[@class="quote"]'
117+ # i = 0
118+ # for quote in g.doc.select(xpath):
119+ # print(quote.text())
120+ # nodes = quote.node.find_class('id')
121+ # if nodes:
122+ # i += 1
123+ # quote_id = int(nodes[0].text.replace('#', ''))
124+ # date = datetime.strptime(quote.node.find_class('date')[0].text, '%Y-%m-%d %H:%M')
125+ # rating = int(quote.node.find_class('rating')[0].text)
126+ # text = quote.node.find_class('text')[0]
127+ # print(dir(text))
128+ # print(text.text_content())
129+ # sys.exit()
130+ # # print(i, Column(quote_id, date, rating, text))
131+ # print(i, quote_id, date, rating, text)
0 commit comments