Python爬虫学习
2014-11-30 09:52
204 查看
这次任务是
写一个程序,以一定时间频率刷新apple store,判断货物有/无状态; 当有货的时候,发一个短信给你手机。
1.首先在网站 http://store.apple.com/hk-zh/buy-iphone/iphone5s上找出所需要的内容。
2.编写相应的代码爬出所要内容。
3.发送短信还不是很清楚,不过发送邮件的方法通过查询可以做到,相应代码如下:
import smtplib
import email
import mimetypes
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def 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()方法来以一定频率刷新。
写一个程序,以一定时间频率刷新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 smtplib
import email
import mimetypes
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def 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()方法来以一定频率刷新。
相关文章推荐
- Python学习笔记 第二部分 - 正则表达式 与 爬虫小实例(抓取豆瓣电影中评分大于等于8分的影片)
- 【爬了个爬——学习Python网络爬虫】1.抓取页面
- Python 爬虫如何入门学习?
- Python 爬虫学习 urllib
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- Python学习笔记之网络爬虫
- 【python爬虫学习经历一】
- python爬虫学习(一)
- Python 学习入门(6)—— 网页爬虫
- Python 爬虫学习 网页图片下载
- python基础学习-7(简单爬虫)
- python 爬虫学习一
- Python 学习入门(6)—— 网页爬虫
- Python 爬虫如何入门学习?
- 使用python2.7学习网络爬虫,问题锦集
- 【爬了个爬——学习Python网络爬虫】0.写在前面的话
- 学习python爬虫
- Python 网页爬虫-BeautifulSoup库的学习
- Python 基础学习 网络小爬虫
- [Python]网络爬虫学习笔记,爬取东南大学SEU-wlan的流量使用情况