Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

python - how to encode properly a Web Scraped content

I wrote a code that detects the amazon stock an print it then I add a function that was supposed to send the stock to the receiver's email address but I get this error: what does it mean and how can I make my code work.

    Traceback (most recent call last):
  File "c:UsersUserDesktopEchinoid ImplementawebinDebug
etcoreapp3.1webscapper.py", line 84, in <module>
    MIMEText(body, "text")
  File "C:UsersUserAppDataLocalProgramsPythonPython39libemailmimeext.py", line 34, in __init__
    _text.encode('us-ascii')
AttributeError: 'NoneType' object has no attribute 'encode'

Also I am using:

-import requests

-from bs4 import BeautifulSoup

-import random

-import email, smtplib, ssl, random

-from email import encoders

-from email.mime.base import MIMEBase

-from email.mime.multipart import MIMEMultipart

-from email.mime.text import MIMEText

Here is the code:

    URL = 'https://finance.yahoo.com/quote/AMZN?p=AMZN'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id="quote-header-info")
for results in soup.find('span'):
    amazon = 'Amazon.com, Inc.'
    span = soup.find('span', {"class": "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)"})
    if span is None:
        span = soup.find('span', {"class": "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($positiveColor)"})
    print('===========')
    percent = span.text
    if "-" in percent:
        print('Amazon.com, Inc. (AMZN) has dropped by: ' + percent)
    elif "+" in percent:
        print('Amazon.com, Inc. (AMZN) has increased by: ' + percent)
    else:
        print('Amazon.com, Inc. (AMZN) has is: ' + percent)
    span1 = soup.find('span', {"class": "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"})
    percent2 = span1.text
    print('===========')
    print('Amazon.com, Inc. (AMZN) stock price is: ' + percent)
    print('===========')
    email_conf = input('Would you Like To recive this information on your email (Y/N): ')
    if email_conf == 'Y':
        def main1():
            if "-" in percent:
                'Amazon.com, Inc. (AMZN) has dropped by: ' + str(percent)
            elif "+" in percent:
                'Amazon.com, Inc. (AMZN) has increased by: ' + str(percent)
            else:
                'Amazon.com, Inc. (AMZN) has is: ' + str(percent)
            return None
           

        print('===========')
        print('Please, wait...')
        print('===========')
        sub = 'Amazon.com, Inc.'
        subject = sub
        body = main1()
        sender_email = input('type your email: ')
        receiver_email = input('type receiver email: ') #Email
        password = "27606805f"

        # Create a multipart message and set headers
        message = MIMEMultipart(body)
        message["From"] = sender_email
        message["To"] = receiver_email
        message["subject"] = subject
        message["Bcc"] = receiver_email  # Recommended for mass emails

        # Add body to email
        MIMEText(body, "plain")

        # Log in to server using secure context and send email
        context = ssl.create_default_context()
        with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
            server.login(sender_email, password)
            server.sendmail(sender_email, receiver_email, body, subject)
        print('===========')
        print('Done...')
        print('===========')
    break

Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...