如何结合webscraper和电子邮件python代码,以便在从scraper创建csv文件后发送该文件?

如何结合webscraper和电子邮件python代码,以便在从scraper创建csv文件后发送该文件?,python,python-3.x,email,web-scraping,email-attachments,Python,Python 3.x,Email,Web Scraping,Email Attachments,我很难找到在我的网络刮板中放置我的电子邮件代码的位置,以便通过电子邮件将我从刮板数据创建的csv文件发送给自己。(对于所有这些仍然是新手,因此我正在学习如何以及在何处将代码放入其他代码中) 我试图在for循环之后运行它,但它似乎不正确。任何帮助都将不胜感激 import requests from bs4 import BeautifulSoup from csv import writer from time import sleep ### email modules ### import

我很难找到在我的网络刮板中放置我的电子邮件代码的位置,以便通过电子邮件将我从刮板数据创建的csv文件发送给自己。(对于所有这些仍然是新手,因此我正在学习如何以及在何处将代码放入其他代码中)

我试图在for循环之后运行它,但它似乎不正确。任何帮助都将不胜感激

import requests
from bs4 import BeautifulSoup
from csv import writer
from time import sleep

### email modules ###
import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders

all_names = []

base_url = "https://www.brownells.com/"
#this is the base url as stated below to help with the page scraper
url = "/optics-mounting/index.htm?f_a=1"
#put first page to scrape url here


#-------making the csv file and titles -------
with open("Brownells_Optics.csv", "w") as csv_file:   #the "w" means to "write" the csv file
    csv_writer = writer(csv_file)
    csv_writer.writerow(["Title", "Price", "Item Url", "Image"])


    #another loop for the pagination and everything below it has to be indented in it
    while url:

        res = requests.get(f"{base_url}{url}")
        print(f"Now Scraping {base_url}{url}...")
        #gives you feedback of what it is scraping when it is running it

        soup = BeautifulSoup(res.text, "html.parser")
        names = soup.findAll("div", {"class": "media listing"})
        #the whole element that is selected

        #looping through all of the names with a for loop
        for name in names:
            title = name.find(itemprop= "name").get_text()
            #print(title)  <--- each one of these were to see if it would print the right thing
            price = name.find(itemprop= "lowPrice").text.strip()
            #print(price)
            item_url = name.find("a").get("href")
            #can also be written name.find("a")["href"]
            #print(item_url)
            img = name.find("img").get("src")
            #print(img)
            #print(title, price, item_url, img)   #doing this to make sure that all three things we are scraping is what we want
            csv_writer.writerow([title, price, item_url, img])
            #needs to be within and below names and above the scraping next page button code in order for it to be within the true statement

        ### Function to send the email ###
        def send_an_email():
            toaddr = 'who to send email to'    
            me = 'from email' 
            subject = "Put subject here"

            msg = MIMEMultipart()
            msg['Subject'] = subject
            msg['From'] = me
            msg['To'] = toaddr
            msg.preamble = "test " 
            #msg.attach(MIMEText(text))

            part = MIMEBase('application', "octet-stream")
            part.set_payload(open("PUT FILE PATH HERE", "rb").read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="PUTFILEHERE.csv"')
            msg.attach(part)

            try:
                s = smtplib.SMTP('smtp.gmail.com', 587)
                s.ehlo()
                s.starttls()
                s.ehlo()
                s.login(user = 'PUT EMAIL HERE', password = 'PUT EMAIL PASSWORD HERE')
            #s.send_message(msg)
                s.sendmail(me, toaddr, msg.as_string())
                s.quit()
            #except:
            #   print ("Error: unable to send email")
            except SMTPException as error:
                print ("Error")

            send_an_email()


        #scraping the next page button
        next_btn = soup.find(class_="next").get("href")
        url = next_btn if next_btn else None
        #telling it to click the next page button and then if there is not one, it wont run anymore, making the text above false and stops it
导入请求
从bs4导入BeautifulSoup
从csv导入编写器
从时间上导入睡眠
###电子邮件模块###
导入smtplib、ssl
从email.mime.multipart导入MIMEMultipart
从email.mime.base导入MIMEBase
从email.mime.text导入MIMEText
从email.utils导入格式日期
从电子邮件导入编码器
所有_名称=[]
基本url=”https://www.brownells.com/"
#这是如下所述的基本url,有助于使用页面刮板
url=“/optics mounting/index.htm?f_a=1”
#将第一页放在这里,以刮取url
#-------制作csv文件和标题-------
打开(“Brownells_Optics.csv”、“w”)作为csv_文件:#“w”表示“写入”csv文件
csv\u writer=writer(csv\u文件)
csv_writer.writerow([“标题”、“价格”、“项目Url”、“图像”])
#另一个分页循环和它下面的所有内容都必须缩进
而url:
res=requests.get(f“{base_url}{url}”)
打印(f“正在刮取{base_url}{url}…”)
#在运行时为您提供有关刮取内容的反馈
soup=BeautifulSoup(res.text,“html.parser”)
names=soup.findAll(“div”,“class”:“媒体列表”})
#选定的整个图元
#使用for循环遍历所有名称
对于名称中的名称:
title=name.find(itemprop=“name”).get_text()

#打印(标题)对
发送电子邮件的唯一调用是在
发送电子邮件
中,这意味着它实际上从未被调用过。

“它似乎是正确的”:请详细说明。web scraper将运行并在我的计算机上生成csv文件,但发送csv文件的代码似乎并没有实际通过电子邮件发送创建的文件。我确保文件也在正确的路径上。我是否缩进或将代码放在错误的区域?“将文件路径放在此处”?我实际上已将该区域和其他区域替换为该文本,以便于其他人阅读您在while循环范围内定义
send_email()
的任何具体原因?整个电子邮件代码是否位于正确的位置,或者你会推荐它在另一个地方吗?另外,如果在合适的位置,你会建议我把“发送电子邮件”放在哪里?我还在努力理解一切,所以我为这些问题道歉