Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.rithmschool.com/blog
- # next_page_url = "https://www.rithmschool.com/blog?page=2"
- import requests
- from bs4 import BeautifulSoup
- from csv import writer
- def get_articles(url):
- response = requests.get(url, timeout=30)
- soup = BeautifulSoup(response.text, "html.parser")
- articles = soup.find_all("article")
- for article in articles:
- a_tag = article.find("a")
- title = a_tag.get_text() # title of each article
- url = a_tag["href"] # url adress of each article
- date = article.find("time")["datetime"] # date of publishing each of the article
- csv_writer.writerow([title, url, date])
- response = requests.get("https://www.rithmschool.com/blog", timeout=30)
- soup = BeautifulSoup(response.text, "html.parser")
- last_page = soup.find("span", class_="last").find("a")["href"]
- site_url = "https://www.rithmschool.com/blog"
- l_page_url = site_url[:-5] + last_page
- pages_range = range(2, (int(l_page_url[-1])) + 2)
- site = "https://www.rithmschool.com/blog"
- page_num = 1
- new_file = "articles.csv"
- print(f"Saving articles from page {page_num}.")
- with open(new_file, "a", newline="") as file:
- csv_writer = writer(file)
- csv_writer.writerow(["title", "link", "date"])
- for num in pages_range:
- get_articles(site)
- site = l_page_url[:-1] + str(num)
- page_num += 1
- print(f"Saving articles from page {page_num}.")
- print(f"All of the articles are saved in {new_file}.")
Advertisement
Add Comment
Please, Sign In to add comment