I'm using requests-html
to scrape a page and some of its text includes special characters, such as \u201D
and \u2022
. Then I'm emailing some data using stmplib
, but I'm getting this error, for example:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 570-571: ordinal not in range(128)
The string of text in question is result_title
.
import os
import smtplib
from requests_html import HTMLSession
session = HTMLSession()
def send_email(type, data):
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
to = 'toemail@example.com'
addresses = [to]
subjectType = f'NEW EBAY RESULT(S) FOR: {type}'
subject = subjectType
body = data
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, addresses, msg)
response = session.get('https://www.ebay.com/sch/i.html?_from=R40&_nkw=blink+182&_sacat=0&_sop=10&rt=nc&LH_PrefLoc=2')
result = response.html.find('ul.srp-results li.s-item')
result_title = result.find('h3.s-item__title', first=True).text.replace('New Listing', '')
send_email(result_title)