-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinfo_engine.py
More file actions
97 lines (74 loc) · 3.27 KB
/
Copy pathinfo_engine.py
File metadata and controls
97 lines (74 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# --*-- coding: utf-8 --*--
import os
import sys
from utils.log import NOTICE, log, ERROR, RECORD
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
sys.path.append(BASE_DIR)
import time
import random
from config import CELERY_BROKER, CELERY_BACKEND, CRAWL_INTERVAL
from db_access import *
from utils.blacklist import blacklist_site, blacklist_company
from utils.content_process import complement_url, check_content
from utils.diff import diff_file
from utils.html_downloader import crawl
from bs4 import BeautifulSoup
from celery import Celery
celery_app = Celery('info_engine', broker=CELERY_BROKER, backend=CELERY_BACKEND)
celery_app.conf.update(CELERY_TASK_RESULT_EXPIRES=3600)
websites = get_websites()
# websites = get_websites_desc()
@celery_app.task
def extract(w_id):
try:
w = get_website(w_id)
# log(NOTICE, "开始 #{id} {name} {site} ".format(id=w.id, name=w.company.name_cn, site=w.url))
new_html_content = crawl(w.url)
if not new_html_content:
log(NOTICE, "#{id} {name} {site} 抓到更新 0 条".format(id=w.company.id, name=w.company.name_cn, site=w.url))
return
if w.html_content:
old_html_content = w.html_content.content
else:
save_html_content(w.id, new_html_content)
log(NOTICE, "#{id} {name} {site} 抓到更新 0 条".format(id=w.company.id, name=w.company.name_cn, site=w.url))
return
diff_text = diff_file(old_html_content, new_html_content)
if not diff_text:
log(NOTICE, "#{id} {name} {site} 抓到更新 0 条".format(id=w.company.id, name=w.company.name_cn, site=w.url))
return
save_html_content(w.id, new_html_content)
soup = BeautifulSoup(diff_text, 'lxml')
items = soup.find_all('a')
COUNT = 0
if items:
for a in items:
if a.string:
url, text = a.get('href'), a.string
check_pass = check_content(url, text)
if check_pass:
url = complement_url(url, w.url)
if url:
result = save_info_feed(url, text, w.id, w.company.id)
if result:
COUNT += 1
# log(RECORD, "[name] [+] [{url} {text}]".format(name=w.company.name_cn, url=url, text=text.strip()))
if COUNT == 0:
log(NOTICE, "#{id} {name} {site} 抓到更新 {count} 条".format(id=w.company.id, name=w.company.name_cn, site=w.url, count=COUNT))
else:
log(RECORD, "#{id} {name} {site} 抓到更新 {count} 条".format(id=w.company.id, name=w.company.name_cn, site=w.url, count=COUNT))
except Exception as e:
try:
w = get_website(w_id)
log(ERROR, "#{id} {name} {site} {err}".format(id=w.id, name=w.company.name_cn, site=w.url, err=str(e)))
except Exception as e:
log(ERROR, str(e))
def gen_info():
# random.shuffle(websites)
for w in websites[:]:
if (w.url not in blacklist_site) and (w.company.name_cn not in blacklist_company):
extract.delay(w.id)
if __name__ == '__main__':
while True:
gen_info()
time.sleep(60 * CRAWL_INTERVAL)