小福利,教大家用python爬取菜单,每天都有新口味

小福利,教大家用python爬取菜单,每天都有新口味,话不多说上代码

import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from email.header import Header

header = {
  'Referer': 'http://www.xiachufang.com/explore/',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
   'Cookie':'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2217192a1d2b9ad-01b2b2d2fb7833-4c302e7f-1327104-17192a1d2ba391%22%2C%22%24device_id%22%3A%2217192a1d2b9ad-01b2b2d2fb7833-4c302e7f-1327104-17192a1d2ba391%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; Hm_lvt_ecd4feb5c351cc02583045a5813b5142=1587303013,1587381431,1589088715; __utma=177678124.368290483.1587303013.1587381432.1589088716.4; __utmz=177678124.1587303013.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __gads=ID=e30de0625a08b155:T=1587303014:S=ALNI_MbzX4cXJfCS7GA10y0jXvZ8zcl2nQ; Hm_lpvt_ecd4feb5c351cc02583045a5813b5142=1589088737; __utmb=177678124.2.10.1589088716; __utmc=177678124; __utmt=1'
    }

def getdata():
    res = requests.get(headers=header,url='http://www.xiachufang.com/explore/rising/')
    print(res.status_code)
    html = res.text
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all(class_="info pure-u")
    list_all=[]
    for item in items:
        name = item.find(class_="name").text.strip()
        a = item.find('a')
        link='http://www.xiachufang.com'+a['href']
        yuanliao = item.find(class_="ing ellipsis").text.strip()

        list_all.append(name+'\n'+link+'\n'+yuanliao+'\n')
        #list_all.append([name.text.strip(),'http://www.xiachufang.com'+a['href'], yuanliao.text.strip()])
        #list_all.append('<'+name+'>'+'<'+link+'>'+'<'+yuanliao+'>')

        #list_all.append('菜名:{}\n链接:{}\n原料:{}\n'.format(name,link,yuanliao))

        #mess=str(list_all)
        #print(name+'\n'+link+'\n'+yuanliao+'\n')
        #print(list_all)
    return '\n\n'.join(list_all)
    #return mess



# account = input('请输入发件人的邮箱:')
# password = input('请输入密码:')
# receiver = input('请输入收件人的邮箱:')



def send_email(list):
    mailhost='smtp.qq.com'
    qqmail = smtplib.SMTP()
    qqmail.connect(mailhost,25)
    qqmail.login(account,password)
    content= '亲爱的,今天的菜单是:'+list
    message = MIMEText(content, 'plain', 'utf-8')
    subject = '今日菜单'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        qqmail.sendmail(account, receiver, message.as_string())
        print ('邮件发送成功')
    except:
        print ('邮件发送失败')
    qqmail.quit()


account = input('请输入你的邮箱')
password = input('请输入你的邮箱密码')
receiver = input('请输入收件人的邮箱')
mess=getdata()
send_email(mess)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值