-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawler.py
More file actions
46 lines (40 loc) · 1.46 KB
/
Copy pathcrawler.py
File metadata and controls
46 lines (40 loc) · 1.46 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
import newspaper
import db
import datetime
from newspaper import news_pool
news_websites = ['https://news.google.com/news/']
def download_articles(news_websites):
papers = []
for site in news_websites:
papers.append(newspaper.build(site))
news_pool.set(papers, threads_per_source=2)
news_pool.join()
for paper in papers:
print("{}: {} articles".format(paper.brand, len(paper.articles)))
return papers
def get_relevant_articles(papers):
for paper in papers:
for article in paper.articles:
insert_db(article)
def insert_db(article):
if article.title is None:
return
title = article.title.replace('"', r'\"')
link = article.url
date = article.publish_date.strftime("%Y-%m-%d") if article.publish_date is not "" else datetime.datetime.now().strftime("%Y-%m-%d")
imgLink = article.top_image if article.top_image is not None else ""
summary = article.text
print("Date: " + date)
print("Url: " + article.url)
print("Title: " + title)
db.insertCrawlerEventIntoTable(date, title, summary, link, imgLink)
def get_summary(text):
summary = ""
# Get first 4 sentences
for sentence in text.split('.')[:5]:
summary += sentence + " "
return text.replace("'", r"\'").replace('"', r'\"')
def run_crawler():
papers = download_articles(news_websites)
get_relevant_articles(papers)
print("Done running crawler.")