Skip to content

[WIP] Leibutina Irina #29

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

Open
wants to merge 2 commits 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
49 changes: 49 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
import requests
from bs4 import BeautifulSoup

# here should be argparse

def parse_description(description):
soup = BeautifulSoup(description, "html.parser")

for anchor in soup.find_all('a'):
if anchor.img:
image_link = anchor.img['src'] # get img url

for anchor in soup.find_all('a', href=True):
link = anchor['href'] #get href urls

context = description[description.rfind("</a>"):]
context = context[:context.find("<p>")].replace("</a>", "")

return [image_link, link, context]

def parse_new(all_news):
all_news_list = []
for current_new in all_news:
title = current_new.find('title').text
date = current_new.find('pubdate').text
description = parse_description(current_new.find('description').text)
all_news_list.append([title, date, description])

return all_news_list



url = 'https://news.yahoo.com/rss/'
r = requests.get(url).text
soup = BeautifulSoup(r, 'html.parser')

all_news = soup.find_all('item')
feed = soup.find('title').text
all_news_list = parse_new(all_news)

print("FEED:", feed)
for new in all_news_list:
print("Title:", new[0])
print("Date", new[1])
print("Description:", new[2][2])
print("Links:")
print(new[2][1], " (link)")
print(new[2][0], "(image)")