Python爬虫学习
来源:互联网 发布:linux怎么退出vi 编辑:程序博客网 时间:2024/05/01 06:26
这次任务是
写一个程序,以一定时间频率刷新apple store,判断货物有/无状态; 当有货的时候,发一个短信给你手机。
1.首先在网站 http://store.apple.com/hk-zh/buy-iphone/iphone5s上找出所需要的内容。
2.编写相应的代码爬出所要内容。
url = 'http://store.apple.com/hk-zh/buy-iphone/iphone5s'while 1: html = requests.get(url) reg = '{"dimensionColor":"(.*?)","dimensionCapacity":"(.*?)","partNumber":".*?","price":"(.*?)","displayShippingQuote":"(.*?)".*?}' r=re.compile(reg) mes = r.findall(html.text) for i in mes: print i[0]+' ',i[1]+' ','HK$'+i[2]+' '+i[3] if i[3].encode('utf-8') == '有現貨': text = 'iphone5s有现货:'+' 颜色:'+i[0].encode('utf-8')+' 内存空间:'+i[1].encode('utf-8')+' 价格:HK$'+i[2].encode('utf-8')
3.发送短信还不是很清楚,不过发送邮件的方法通过查询可以做到,相应代码如下:
import smtplibimport emailimport mimetypesfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextdef sendmail(to_list,sub,con): """发送邮件 """# 设置服务器名称、用户名、密码以及邮件后缀s mail_host="smtp.163.com" mail_user="*****@163.com" mail_pass="*****" mail_postfix="mail.163.com" me = mail_user+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEMultipart('related') msg['Subject'] = email.Header.Header(sub,'utf-8') msg['From'] = me msg['To'] = ";".join(to_list) msg.preamble = 'This is a multi-part message in MIME format.' msgAlternative = MIMEMultipart('alternative') msgText = MIMEText(con, 'plain', 'utf-8') msgAlternative.attach(msgText) msg.attach(msgAlternative) try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() except Exception,e: return False return True
4.通过将两个方法结合便可以初步完成目标。最后可以用time.sleep()方法来以一定频率刷新。
0 0
- 学习python爬虫
- python 爬虫学习一
- Python爬虫学习
- Python爬虫学习
- python爬虫学习
- Python学习--爬虫
- python学习爬虫
- Python 爬虫学习1
- Python 爬虫学习2
- python简单爬虫学习
- Python简单爬虫学习
- Python爬虫学习系列
- python + 机器学习 + 爬虫
- python 爬虫 学习
- python爬虫基础学习
- Python爬虫学习总结
- Python爬虫学习
- python beautifulsoup 爬虫学习
- PJSIP开发笔记
- MySQL(no installer)完整安装过程
- (每日算法)LeetCode---Minimum Window Substring (最小子串窗口)
- PrepareStatement和Statement
- ofbiz ECA定义和使用
- Python爬虫学习
- 【恶补字符串Project】
- openssl生成私钥和公钥
- 公钥 私钥
- 第13周项目5(2)去除字符串空格
- Sublime Text 3 license 一个 供研究使用
- 内部类
- hduoj-2084 数字塔问题 DP
- linux下的patch补丁包实例详解以及diff命令实例。