|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import re |
| 4 | +from .base import BaseScraper |
| 5 | +from logger_config import get_logger |
| 6 | +from email.utils import parsedate_to_datetime |
| 7 | +from dateutil import parser |
| 8 | +from datetime import timezone |
| 9 | + |
| 10 | +BASE_URL = 'https://engineering.linkedin.com/blog' |
| 11 | +HEADERS = {'User-Agent': 'Mozilla/5.0'} |
| 12 | + |
| 13 | +logger = get_logger("linkedin-heandler") |
| 14 | +class LinkedinScraper(BaseScraper): |
| 15 | + def scrape(self): |
| 16 | + pass |
| 17 | + |
| 18 | + def get_posts_from_group_url(self, url, last_scan_time): |
| 19 | + logger.debug(f"Getting posts from group url: {url}") |
| 20 | + resp = requests.get(url, timeout=5) |
| 21 | + if resp.status_code != 200: |
| 22 | + logger.warning(f"Non-200 response for {url}: {resp.status_code}") |
| 23 | + return None |
| 24 | + |
| 25 | + soup = BeautifulSoup(resp.text, "html.parser") |
| 26 | + |
| 27 | + posts = [] |
| 28 | + |
| 29 | + post_items = soup.find_all("li", class_="post-list__item grid-post") |
| 30 | + |
| 31 | + for post in post_items: |
| 32 | + try: |
| 33 | + # Title |
| 34 | + title_tag = post.find("div", class_="grid-post__title") |
| 35 | + if title_tag and title_tag.a: |
| 36 | + title = title_tag.a.get_text(strip=True) |
| 37 | + url = title_tag.a["href"] |
| 38 | + else: |
| 39 | + logger.exception("Title not found") |
| 40 | + continue |
| 41 | + |
| 42 | + # Topic |
| 43 | + topic_tag = post.find("p", class_="grid-post__topic") |
| 44 | + topic = [topic_tag.a.get_text(strip=True) if topic_tag and topic_tag.a else ""] |
| 45 | + |
| 46 | + # Published date |
| 47 | + date_tag = post.find("p", class_="grid-post__date") |
| 48 | + if date_tag: |
| 49 | + published = parser.parse(date_tag.get_text(strip=True)) |
| 50 | + if published.tzinfo is None: |
| 51 | + published = published.replace(tzinfo=timezone.utc) |
| 52 | + else: |
| 53 | + published = None |
| 54 | + |
| 55 | + if not published: |
| 56 | + logger.exception("Published date not found") |
| 57 | + continue |
| 58 | + |
| 59 | + if last_scan_time.tzinfo is None: |
| 60 | + last_scan_time = last_scan_time.replace(tzinfo=timezone.utc) |
| 61 | + |
| 62 | + # breaking the loop when first article appears having stale |
| 63 | + if published <= last_scan_time: |
| 64 | + logger.debug(f"Skipping post: {title} as it is published on {published} before {last_scan_time}") |
| 65 | + break |
| 66 | + |
| 67 | + posts.append({ |
| 68 | + "title": title, |
| 69 | + "url": url, |
| 70 | + "tags": topic, |
| 71 | + "published": published.isoformat() |
| 72 | + }) |
| 73 | + |
| 74 | + # Stop early after collecting 2 items |
| 75 | + if len(posts) >= 2: |
| 76 | + break |
| 77 | + |
| 78 | + except Exception as e: |
| 79 | + logger.exception(f"Error parsing group post") |
| 80 | + continue |
| 81 | + |
| 82 | + return posts |
| 83 | + |
| 84 | + |
| 85 | + def search_blog_posts(self, category, last_scan_time): |
| 86 | + res = requests.get(BASE_URL) |
| 87 | + soup = BeautifulSoup(res.text, "html.parser") |
| 88 | + |
| 89 | + posts = [] |
| 90 | + groups = soup.select(".artdeco-dropdown__content") |
| 91 | + |
| 92 | + for group in groups: |
| 93 | + links = group.select(".artdeco-dropdown__item a.header-nav__link") |
| 94 | + |
| 95 | + links = links |
| 96 | + |
| 97 | + for grouplink in links: |
| 98 | + try: |
| 99 | + group_url = grouplink.get("href") |
| 100 | + |
| 101 | + group_posts = self.get_posts_from_group_url(group_url, last_scan_time) |
| 102 | + |
| 103 | + for post in group_posts: |
| 104 | + posts.append(post) |
| 105 | + except: |
| 106 | + logger.exception("Failed while scraping Linkedin") |
| 107 | + |
| 108 | + return posts |
| 109 | + |
0 commit comments