close
小偏用維骨力做範例
來爬蟲
import requests from bs4 import BeautifulSoup import smtplib from email.mime.text import MIMEText def check_price(url): resp=requests.get(url) soup=BeautifulSoup(resp.text,'html.parser') words = list(soup.find('div', 'product-price-detail').stripped_strings) print(words) print("nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn") # e.g., ['$1,529', '$26 / 1粒', '(含運)'] # ['商品原價', '$1,529', '商品折扣', '-', '$240', '小計', '$1,289', '$22 / 1粒', '(含運)'] #prices = [w for w in words if '$' in w] prices=[] for w in words: if '$' in w: prices.append(w) print(prices) return '折扣' in ' '.join(words), prices[-2] def send_mail(price): EMAIL = '你的gmail' PWD = '你的密碼' sender = to = EMAIL subject='維骨力 {}'.format(str(price)) #subject = 'p body = 'https://www.costco.com.tw/Health-Beauty/Vitamins-Herbals-Dietary-Supplements/Glucosamine-Joint-Supplements/Move-Free-Ultra-60-Tablets/p/363985' msg = MIMEText(body.encode('utf-8'), _charset='utf-8') msg['Subject'] = subject msg['From'] = sender msg['To'] = to try: server = smtplib.SMTP_SSL('smtp.gmail.com', 465) server.ehlo() server.login(EMAIL, PWD) server.sendmail(sender, to, msg.as_string()) server.close() print('Email 已寄出') except Exception as e: print('Email 寄送失敗') print(e) if __name__ == '__main__': urls = [ 'https://jwlin.github.io/py-scraping-analysis-book/ch8/move-free-normal.html', # 普通網頁 'https://jwlin.github.io/py-scraping-analysis-book/ch8/move-free-discount.html' # 折扣網頁 ] print(check_price(urls[0])) print(check_price(urls[1])) is_discount, price = check_price(urls[1]) if is_discount: send_mail(price)
全站熱搜