Exploring the possibilities of utilizing Selenium variables in Python 3 to automate email creation

Hi there! This is my first post and I'm fairly new to Python, especially when it comes to sending emails from it. My current objective is to scrape the first 5 articles from a website and send them to my email address. Right now, all I need is for it to work once, sending from one email address to another. Scheduling can come later - let's focus on solving this issue first. Below you'll find the code that scrapes articles using Selenium. Is there any way to modify it so that it prints out 5 different news stories instead of the same story repeated?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from webdriver_manager.chrome import ChromeDriverManager


def getnews():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://community.elitedangerous.com/en/")
    driver.implicitly_wait(5)
    news = driver.find_element_by_class_name("article")
    divider =("_____________________________________________________________________________________________________")
    for i in range(0,6):
        print(news.text)
        print(divider)
    driver.quit()


getnews()

If you run this code, you'll receive nerdy news. I also have an email code snippet that works well with basic text:

import smtplib


with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login('mysendingemail.gmail.com', 'myverysecurepassword')

    subject = "this is the title"
    body = "this is the body of the email"

    msg = f'Subject: {subject}\n\n{body}'

    smtp.sendmail('mysendingemail.gmailcom', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc9185938894998e99919d9590bc9b919d9590d29f9391">[email protected]</a>',msg)

My question is: How can I extract text from a website and send it via email? Any pointers on scheduling weekly emails would be much appreciated.

After receiving some feedback about creating a list, I made some updates to the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from webdriver_manager.chrome import ChromeDriverManager
import smtplib



def get_news():
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://community.elitedangerous.com/en/")
    driver.implicitly_wait(5)

    v=2

    divider =("_____________________________________________________________________________________________________")
    news_list = []
    for x in range(0, 5):
        if v <= 7:
            xpath = (str('//*[@id="block-system-main"]/div[' + str(v) + ']'))
            news = driver.find_element_by_xpath(xpath)
            body = news.text
            news_list.append(news.text)

            v += 1
        else:
            break

    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a392f242e2f382f272b23262a38073977">[email protected]</a>', 'password for email')

        subject = "news"
        body = str(news_list)

        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail('sender email.com', 'recieveremail.com', msg)


get_news()

Upon running this updated code, I encountered a UnicodeEncodeError. Is this because the list isn't converting into a string properly?

Answer â„–1

The title variable represents the subject of the message, while the content variable contains the actual message body. To help visualize this concept, refer to the illustration below:

https://i.stack.imgur.com/Y7T2W.png

Answer â„–2

Instead of manually extracting information from news articles, consider storing the content in an array of strings for easier processing. Your getnews() function can then return this array, which can be called in your email sending script. Let me know if you need further clarification!

Just a friendly suggestion: in Python, it's common practice to use snake_case for functions and variables. Therefore, renaming getnews() to get_news() would align with Python conventions. It's a small detail, but adhering to the language's norms is always beneficial.

Answer â„–3

Display the first 5 items from the list.

news_stories = driver.find_elements_by_class_name("article")
for story in news_stories[:5]:
    print(story.text)
    print(separator)

It is recommended to store them in a list.

stories_list=[]
news_stories = driver.find_elements_by_class_name("article")
for story in news_stories[:5]:
    stories_list.append(story.text)

You can then easily send your list through a message.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extracting timestamped text data from a simulated chat interface

I am looking to gather chat data from Twitch clips. These are saved moments from livestreams where viewer reactions are captured. Take a look at this example clip: While I can scrape all the data by watching the video and utilizing query selectors, my goa ...

How to utilize method decorators effectively in C# programming

One interesting feature in python is the ability to use function decorators to enhance the functionality of functions and methods. Currently, I'm in the process of transitioning a device library from python to C#. During communication with the device ...

Utilizing pandas and numpy to import a CSV file and transform it from a two-dimensional vector to a one-dimensional vector while excluding any diagonal elements

Here is the content of my csv file: 0 |0.1|0.2|0.4| 0.1|0 |0.5|0.6| 0.2|0.5|0 |0.9| 0.4|0.6|0.9|0 | I am attempting to read it row by row, excluding the diagonal values, and converting it into a single long column: 0.1 0.2 0.4 0.1 0.5 0.6 0.2 0.5 0.9 ...

What are the best ways to locate elements with changing IDs using Selenium?

I am looking to create a basic script that can generate an email account on the website: Every time I refresh the page, the input text fields have different IDs. How can I reliably locate these elements? And why would the ID be changed constantly? For in ...

Exploring a search function using Python's unittest module

I'm currently working on writing unit tests for my search function, which scans through a directory and provides a list of matching files based on the query. However, I'm facing an issue with using test data as it may occupy too much space. What ...

Utilizing C# Windows Forms to Engage With a Web Browser

Currently, I have created a Windows Form application using C# that is able to communicate with a phone system. My next goal is to incorporate click-to-dial capabilities into the application. The idea is that when a telephone number is clicked on in a web ...

What is the best way to display 0xfb in Python programming language?

It feels like I'm diving headfirst into the depths of unicode hell. My setup is on a Unix system running Python 2.7.3 LC_CTYPE=zh_TW.UTF-8 LANG=en_US.UTF-8 I'm attempting to convert hex encoded data into a human-readable format, and here' ...

Selenium in Python struggling to locate dynamically loaded JavaScript table following automated login process

I am currently utilizing Selenium with Python3 to navigate a Service Now Website. The process is as follows: Selenium loads the ServiceNow URL and I use sendKeys to automate entering the username and password. Once the page is loaded, there is a table of ...

Combine lines by adding "LF" until encountering "CRLF"?

I need help resolving a file cleanup issue. The problem involves a "*.csv" file that contains multiple lines, all ending with "\CR\LF". Occasionally, some lines are broken and only end with "\LF", missing the "\CR". My goal is to consol ...

Selenium Visualiser with Offset Coordinates

After mastering the move_by_offset(x, y).perform() command, I find myself questioning the significance of x and y. Are these coordinates that we plot on an axis where x and y represent their respective positions? ...

Is there a way to move all the elements in a nested python list in a downward direction?

In our scenario, we have a 4x4 grid with empty spaces and 1s grid = [ [" "," ","1"," "], [" "," ","1"," "], ["1","1"," "," "], ["1& ...

If the MacBook is locked, Jenkins-initiated Selenium Safari tests will be unable to run on the device

After ensuring that the settings in system preferences prevent the mac from sleeping, Jenkins initiates a scheduled test. The browser opens but the test fails due to a blank page. If the mac is unlocked or the screensaver is not active, the same test runs ...

How to Retrieve Chrome's Download Folder Using ChromeDriver

I’ve been utilizing Selenium ChromeDriver and am running into an issue with identifying the download directory for Chrome in my Python script. The script collects files from a website, but each user may have a different download location set up. Is the ...

Converting JSON data from an API file into a CSV format

Currently, I am working on converting the JSON output received from an API request into a CSV format for storage in our database. Below is the code snippet I am using for reference: // Python code here... Sample data obtained from the API: // Sample API ...

Displaying historical data in Django

My code is quite straightforward; it accesses the database to retrieve a list of upcoming events. now = datetime.datetime.now(pytz.utc) def index(request, listing='upcoming'): country_name = get_client_ip(request) if Location.objects. ...

What is the origin of the character?

Currently running a string test. When I obtain the innerHTML, there are unexpected u'\n and \n', AssertionError: u'\n<p><strong>The user/password code entered is incorrect!</strong></p>\n' != ...

What causes a blank page to appear in Firefox upon initial execution with Selenium?

from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() #driver.set_preference("browser.startup.homepage_override.mstone", "ignore") driver.get("https://url.aspx/") username = driver.find_element_by_name ...

Access a compressed file (.gz) stored in Google Cloud storage using Python within a Jupyter notebook

I'm attempting to access a .gz file stored in Google Cloud storage using Python within Jupyter notebook. The first code snippet throws an error: TypeError: can't concat str to bytes from google.cloud import storage import pandas as pd from io ...

Converting Byte strings to Byte arrays in Node.js using JavaScript

Currently facing a challenge while trying to complete the pythonchallenge using JS and Node. Stuck on challenge 8 where I need to decompress a string using bzip2: BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x ...

Facing difficulties in initiating Selenium with PhantomJS/GhostDriver as child processes

I am currently working on a Node script that involves using the child_process module in order to run a Selenium server with PhantomJS's GhostDriver. I have imported the module: Child = require "child_process" This is how I am attempting to start the ...