summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <yuv.adm@gmail.com>2013-12-22 13:13:43 +0200
committerYuval Adam <yuv.adm@gmail.com>2013-12-22 13:13:43 +0200
commite4657d0b1d31d0d42a573987ccfb4f297b6c204f (patch)
tree413ba89d7cd618f693232ac72657e2b63378485e
parentf2cfd982e1b34a384c649b83bba34bffabc4decb (diff)
Initial homepage parsing
-rw-r--r--newsdiff/core/tasks.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/newsdiff/core/tasks.py b/newsdiff/core/tasks.py
index d22de2b..8b6123a 100644
--- a/newsdiff/core/tasks.py
+++ b/newsdiff/core/tasks.py
@@ -8,13 +8,34 @@ from pytz import timezone
from .models import HaaretzArticle, HaaretzImage
from .utils import get_image_from_url
+DEFAULT_HEADERS = {
+ 'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
+}
+
ISRAEL_TIMEZONE = timezone('Asia/Jerusalem')
+def is_haaretz_href(href):
+ if href is None:
+ return False
+ if href.startswith('http://www.haaretz.co.il/'):
+ return True
+ if href.startswith('/news/'):
+ return True
+ # there are more, such as /{gallery,captain}/, etc.
+ # but ignore for now
+ return False
+
+@app.task():
+def parse_haaretz_homepage():
+ req = requests.get('http://www.haaretz.co.il/', headers=DEFAULT_HEADERS)
+ if req.ok:
+ soup = BeautifulSoup(req.text, 'lxml')
+ articles = soup.findall('a', href=is_haaretz_href)
+
+
@app.task()
def get_haaretz_article(url):
- req = requests.get(url, headers={
- 'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
- })
+ req = requests.get(url, headers=DEFAULT_HEADERS)
if req.ok:
soup = BeautifulSoup(req.text, 'lxml')
title = soup.find('h1', class_='mainTitle').text.strip()