|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +from selenium import webdriver |
| 4 | +from selenium.webdriver.common.keys import Keys |
| 5 | +import time |
| 6 | +from fpdf import FPDF |
| 7 | + |
| 8 | +# Get input for category and number of articles |
| 9 | +category = input("Enter category (Ex- Programming or javascript) : ") |
| 10 | +number_articles = int(input("Enter number of articles: ")) |
| 11 | +driver_path = input("Enter chrome driver path: ") |
| 12 | + |
| 13 | +url = 'https://medium.com/topic/{}'.format(category) |
| 14 | + |
| 15 | +# initiating the webdriver to run in incognito mode |
| 16 | +chrome_options = webdriver.ChromeOptions() |
| 17 | +chrome_options.add_argument("--incognito") |
| 18 | +driver = webdriver.Chrome(driver_path, options=chrome_options) |
| 19 | +driver.get(url) |
| 20 | + |
| 21 | +# this is just to ensure that the page is loaded |
| 22 | +time.sleep(5) |
| 23 | +html = driver.page_source |
| 24 | + |
| 25 | +# Now apply bs4 to html variable |
| 26 | +soup = BeautifulSoup(html, "html.parser") |
| 27 | +articles = soup.find_all('section') |
| 28 | + |
| 29 | +# Getting articles from medium |
| 30 | +num = number_articles |
| 31 | +for article in articles: |
| 32 | + article_data = article.find('a')['href'] |
| 33 | + if article_data[0] == '/': |
| 34 | + article_data = 'https://medium.com' + article_data |
| 35 | + |
| 36 | + post_url = article_data |
| 37 | + driver.get(post_url) |
| 38 | + time.sleep(5) |
| 39 | + |
| 40 | + post_html = driver.page_source |
| 41 | + soup = BeautifulSoup(post_html, "html.parser") |
| 42 | + a_tags = soup.find_all('a') |
| 43 | + |
| 44 | + author = a_tags[2].text |
| 45 | + |
| 46 | + title = soup.find('h1').text.strip() |
| 47 | + section = soup.find_all('section')[1] |
| 48 | + p_tags = section.find_all('p') |
| 49 | + |
| 50 | + title_string = (title).encode( |
| 51 | + 'latin-1', 'replace').decode('latin-1') |
| 52 | + author_string = (author).encode('latin-1', 'replace').decode('latin-1') |
| 53 | + |
| 54 | + # Add a page in pdf |
| 55 | + pdf = FPDF() |
| 56 | + pdf.add_page() |
| 57 | + # set style and size of font for pdf |
| 58 | + pdf.set_font("Arial", size=12) |
| 59 | + |
| 60 | + # Title cell |
| 61 | + pdf.cell(200, 5, txt=title_string, ln=1, align='C') |
| 62 | + # Author cell |
| 63 | + pdf.cell(200, 10, txt=author_string, ln=2, align='C') |
| 64 | + |
| 65 | + for p_tag in p_tags: |
| 66 | + article_part = (p_tag.text.strip()).encode('latin-1', 'replace').decode('latin-1') |
| 67 | + article_part += '\n' |
| 68 | + # Add part of article to pdf |
| 69 | + pdf.multi_cell(0, 5, txt=article_part, align='L') |
| 70 | + |
| 71 | + # save the pdf with name .pdf |
| 72 | + pdf_title = ''.join(e for e in title if e.isalnum()) |
| 73 | + pdf.output("{}.pdf".format(pdf_title)) |
| 74 | + |
| 75 | + num = num-1 |
| 76 | + if(num == 0): |
| 77 | + break |
| 78 | + |
| 79 | + |
| 80 | +driver.close() # closing the webdriver |
0 commit comments