I want to extract the Python list and send it by email.

Asked 2 years ago, Updated 2 years ago, 38 views

After scraping on a specific web page using selenium/BeautifulSoup in Python 3, we extracted list character data.
Since then, I have sent the extracted list by email.
However, all of the list printed in the message is not sent to the email as shown below.
Do you know why?

Could you give me guidance?

Retrieved list
message

Phone number: 0701232511
IMEI: 812341050123456
ICCID: 81234560074626223089
5GSINR: 19.5 dB
5G RSRP: -102 dBm
Network name (2.4 GHz band): TEST_L10_123ECF
Network Name (5 GHz band): TEST_L12_234ECF_5G
Maximum number of accesses (5 GHz band main SSID): 30
Maximum connections: 40
Wi-Fi coverage:long distance mode
IP address: 192.111.1.1
WAN-side IP address: 11.211.13.121
IPv6 address on WAN side: 2111:0123:c123:1230:f4fc:12e3:1231:1b12
Software version: 1.0.5_U

US>List Sent to Mail
message

Software version: 1.0.5_U

Code

 from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
from datetime import datetime as dt, date, timedelta
import pyautogui
import pyperclip
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import urlib3
from torch import nn
from urllib3.exceptions importInsecureRequestWarning 
urllib3.disable_warnings(InsecureRequestWarning)
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
from email.mime.text import MIMEText
from email import encoders, message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
import smtplib
from email.utils import formatdate

#headless background 
option=Options()
option.add_argument('--headless')

# Getting Default Adapter Failed error message
option.add_experimental_option('excludeSwitches', ['enable-logging'])

strDate=dt.now().strftime("%Y%m%d")

today=dt.today()
(dt.strftime(today, '%Y-%m-%d')

yesterday=today-timedelta(days=28)
(dt.strftime(yesterday, '%Y-%m-%d')

URL = "http://192.111.1.1/index.html#test"

# Open your browser. # options=optionbackground 
driver=webdriver.Chrome(executable_path="C:\\Users\\sun\\Documents\\python\\chromedriver.exe", options=option)

# Open Google's top search screen.
driver.get (URL)
# Wait 3 seconds
time.sleep(3)

# Enter Password
password = driver.find_element_by_name("txtPwd")
password.send_keys("1234")

# Click the login button
login=driver.find_element_by_xpath('//*[@id="btnLogin"]')
login.click()

time.sleep(3)
# Click the showDetailInfo button
showDetailInfo=driver.find_element_by_xpath('//*[@id="showDetailInfo"]')
showDetailInfo.click()


time.sleep(3)
# Get HTML for the page you want to search for

html=driver.page_source.encode('utf-8')

soup = BeautifulSoup(html, 'lxml')
elements = [tag.text for tag in soup]
print(elems)


# Detailed information Retrieves only a list of software versions from terminal information
import re
lst = re.split(r'\n+',elems[0])
lst = lst [lst.index('Advanced Terminal Information') +1:]
lst = lst [:lst.index('software version')+2]

attr=dict()
for key, val in zip (*[iter(lst)]*2):
  attr [key] = val
  # Confirm list acquisition
  # print(attr)

# Between the list of items: Add
fork, vin attr.items():
   #news=attr.replace("Phone", "")
   message=k+':'+v
   print(message)

# Send mail
sendAddress='example@'
password = '124'
subject="[Terminal Information]"
bodyText=message
fromAddress='example@'
toAddress='example@'

# Connecting to an SMTP Server
smtpobj=smtplib.SMTP ('smtp.gmail.com',587)
smtpobj.starttls()
smtpobj.login(sendAddress, password)

# mail creation
msg = MIMEText (bodyText)
msg ['Subject'] = subject
msg ['From'] = fromAddress
msg ['To'] = toAddress
msg ['Date'] = formatdate()

# Send created email
smtpobj.send_message(msg)
smtpobj.close()
            

python python3

2022-09-30 17:12

1 Answers

I was able to solve this problem using the following method.
①Add join to concatenate characters

Example: code

list=['My', 'name', 'is', 'Yamada', 'Taro']
str = ''.join(list)
print(str)

Run Results

My name is Taro Yamada

改Add '\n' to break the line

for Add for and connect the characters of each item

Corrected

message='\n'.join(k+':'+v fork, vin attr.items())


2022-09-30 17:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.