Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added publication date and error handling #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions Scripts/techStream/techStream.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@

# Loop through each URL and scrape the latest articles
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.content, features="xml")

# Extract the title, link, and summary of the latest articles
articles = soup.find_all("item")[:5]
for article in articles:
title = article.find("title").text
link = article.find("link").text
summary = article.find("description").text

# Add the title, link, and summary to the HTML document
html += "<h2><a href='" + link + "'>" + title + "</a></h2>"
html += "<p>" + summary + "</p>"
try:
response = requests.get(url)
response.raise_for_status() #check for successful request
soup = BeautifulSoup(response.content, features="xml")
# Extract the title, link, and summary of the latest articles
articles = soup.find_all("item")[:5] #latest 5 articles
for article in articles:
title = article.find("title").text
link = article.find("link").text
summary = article.find("description").text

# extracting publivcation date(if there)
pub_date=article.find("pubDate")
pub_date = pub_date.text if pub_date else "No Date Available"

# Add the title, link, summary and publication date to the HTML document
html += f"<h2><a href='{link}'>{title}</a></h2>"
html += f"<p><strong>Published on: {pub_date}</strong></p>"
html += f"<p>{summary}</p>"
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")



# Close the HTML document
html += "</body></html>"
Expand Down