Skip to content
Open
Show file tree
Hide file tree
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
58 changes: 48 additions & 10 deletions 2.Automate The News/1.news-extract-data.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd

web = 'https://www.thesun.co.uk/sport/football/'
path = '/Users/frankandrade/Downloads/chromedriver' # introduce path here
path = "C:\\Users\\LENOVO\\Downloads\\chromedriver-win64 (1)\\chromedriver-win64\\chromedriver.exe" # introduce path here

# Creating the driver
driver_service = Service(executable_path=path)
driver = webdriver.Chrome(service=driver_service)
driver.get(web)

driver.maximize_window()
# Finding Elements
containers = driver.find_elements(by='xpath', value='//div[@class="teaser__copy-container"]')
containers = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, '//div[@class="teaser__copy-container"]'))
)

titles = []
subtitles = []
links = []
for container in containers:
title = container.find_element(by='xpath', value='./a/h2').text
subtitle = container.find_element(by='xpath', value='./a/p').text
link = container.find_element(by='xpath', value='./a').get_attribute('href')
titles.append(title)
subtitles.append(subtitle)
links.append(link)
# print(container)
try:
highligted_title = container.find_element(by='xpath', value='./a/span').text
except NoSuchElementException:
print("highligted_title Element is not founded")
try:
title = container.find_element(by='xpath', value='./a/h3').text

if highligted_title:
text = highligted_title+' '+title
titles.append(text)
else:
titles.append(title)

except NoSuchElementException:
print("Title Element is not founded")
try:
subtitle = container.find_element(by='xpath', value='./a/p').text
subtitles.append(subtitle)
except NoSuchElementException as e:
print("Subtitle Element is not founded")
try:
link = container.find_element(by='xpath', value='./a').get_attribute('href')
links.append(link)
except NoSuchElementException:
print("Link Element is not founded")


# Exporting data to a CSV file
my_dict = {'title': titles, 'subtitle': subtitles, 'link': links}
# Find the minimum length of the lists
min_length = min(len(titles), len(subtitles), len(links))

# Trim all lists to the same length
titles = titles[:min_length]
subtitles = subtitles[:min_length]
links = links[:min_length]

# Create DataFrame
my_dict = {"Title": titles, "Subtitle": subtitles, "Link": links}
df_headlines = pd.DataFrame(my_dict)
df_headlines.to_csv('headline.csv')

Expand Down
11 changes: 11 additions & 0 deletions 2.Automate The News/headline.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
,Title,Subtitle,Link
0,HAPPY BLUE YEAR Enzo Fernandez's ex-fiancee flies to London to spend NYE with Chelsea star,THE Blues midfielder stunned everyone including the pretty brunette by telling her earlier this year he wanted to end their six-year relationship.,https://www.thesun.co.uk/sport/32513884/enzo-fernandez-valentina-cervantes-new-year-chelsea/
1,JIM BIG TROUBLE Man Utd staring £10million black hole in the face as Ratcliffe cuts costs,"FANS appalled to discover the Old Trafford club has terminated £40,000 of payments to a charity helping some of its older players",https://www.thesun.co.uk/sport/32515322/man-utd-10m-penalty-charity-cuts/
2,WIFE MATERIAL Cristiano Ronaldo's slip of the tongue sparks Georgina relationship rumours,THE superstar singer was in north London to watch on as Arsenal jumped back up to second place in the Premier League thanks to a 1-0 victory over Ipswich Town.,https://www.thesun.co.uk/sport/32513329/cristiano-ronaldo-calls-georgina-rodriguez-wife-awards/
3,BUKAYO BLOW Saka undergoes surgery and set for months out in huge Arsenal injury blow,A gunman shot his pistol at a footballer during a mass brawl at a teenage match in Argentina.,https://www.thesun.co.uk/sport/32511012/bukayo-saka-surgery-mikel-arteta-arsenal-injury-ipswich/
4,NYD DRAW Win a Lamborghini or £300k cash alternative from just 71p with our discount code,SUPERSTAR believes the problems at the club run deeper than whichever manager is in charge.,https://www.thesun.co.uk/sport/22922880/win-car-competition-7dp/
5,MEAN UNITED Man Utd fans blast Jim Ratcliffe after he cuts £40k donation to club charity,GUNNERS took the lead midway through the first half through German's close-range finish. They did not add to their tally but it was enough for an important three points.,https://www.thesun.co.uk/sport/32507962/manchester-united-sir-jim-ratcliffe-charity-donations/
6,RON THE MOVE? Ronaldo opens door to shock Man City transfer with cryptic reply to question,THE Red Devils spent Christmas in the bottom half of the Premier League table for the first time. But how might they fare come the end of the season?,https://www.thesun.co.uk/sport/32514796/transfer-news-cristiano-ronaldo-man-city/
7,COUNT ON ROO Wayne Rooney breaks silence on facing sack with Argyle's form the worst in EFL,"MAN City star's kids race round his Cheshire mansion’s garden in electric Christmas gifts modelled on his own £200,000 car",https://www.thesun.co.uk/sport/32504254/wayne-rooney-plymouth-argyle-sack/
8,RU'D AWAKENING Amorim concerned with Onana as Man Utd boss plots keeper signing,JUVENTUS have rejected an offer to sign Rashford. The Serie A giants are instead keen on Rashford's Man Utd team-mate.,https://www.thesun.co.uk/sport/32506485/ruben-amorim-andre-onana-transfer-senne-lammens/
9,GONE TO POTT Stoke sack manager after just three months in ruthless Christmas axing,BOSS gives shock response when asked about his on-pitch targets this season. And he explained where he reckons the Red Devils went wrong at Wolves.,https://www.thesun.co.uk/sport/32507884/stoke-sack-narcis-pelach-efl/