Iphone_PassMarkRating.py

#coding=utf-8
#---------------------------------------
# 程序:iphonebenchmark爬虫
# 作者:ewang
# 日期:2016-7-11
# 语言:Python 2.7
# 功能:获取页面中的iphone PassMark Rating信息保存到文件中。
#---------------------------------------

import string
import urllib2
import re
import os

class iphonebenchmark_Spider:
#申明相关属性
def __init__(self,url):
#给SougoPicUrl属性赋值
self.iphonebenchmarkUrl=url
#用来保存图片URL信息
self.iphonebenchmark=[]
print u'爬虫,爬爬...'

#初始化加载页面并将其转码存储
def iphoneBenchMark(self):
#读取页面的原始信息
Page=urllib2.urlopen(self.iphonebenchmarkUrl).read()

#获取页面标题
title=self.find_title(Page)
print u'网页名称:'+title

#获取页面中文本信息
self.save_infor(title)

#查找页面标题
def find_title(self,page):
#匹配<title>xxxx</title>
myTitle=re.search(r'<title>(.*?)</title>',page,re.S)

#初始化标题名为暂无标题
title=u'暂无标题'

#如果标题存在把标题赋值给title
if myTitle:
#(.*?)这称作一个group,组是从1开始
title=myTitle.group(1)
else:
print u'爬虫报告:无法加载网页标题...'
return title

#保存页面信息
def save_infor(self,title):
#加载页面文本信息到数组中
self.get_infor()

#创建并打开本地文件
f=open(title+'.csv','w+')

#把获取的页面信息写入文件中
f.writelines(self.iphonebenchmark)

#关闭打开的文件
f.close()
print u'爬虫报告:文件'+title+'.csv'+u'已经下载:'+os.getcwd()
print u'按任意键退出...'
raw_input()


#获取页面源码并将其存储到数组中
def get_infor(self):
#获取页面中的源码
page=urllib2.urlopen(self.iphonebenchmarkUrl).read()

#把页面中所有jpg图片的URL提取出来
self.deal_iphone_Device(page)



def deal_iphone_Device(self,page):
#获取所有设备名称
iphone_Device=re.findall('\<a href=\"phone\.php\?phone=(.*?)\"\>',page,re.S)

#把手机型号的添加到iphonebenchmark列表中
for aItem in iphone_Device:

self.iphonebenchmark.append(aItem)

BenchMark.py


#coding=utf-8
import MySQLdb
from Iphone_PassMarkRating import iphonebenchmark_Spider

class BenchMark:
def Iphone(self,values):
try:

conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',port=3306,charset="utf8")

cur=conn.cursor()



cur.execute('create database if not exists BenchMark')

conn.select_db('BenchMark')

cur.execute('create table if not exists Iphone(info varchar(100))')

cur.execute('insert into Iphone values(%s)',values)
n=cur.execute('select * from Iphone')
print 'select',n

conn.commit()

cur.close()

conn.close()

except MySQLdb.Error,e:

print "Mysql Error %d: %s" % (e.args[0], e.args[1])
print u'需要爬取得URL(passmark_chart,memmark_chart,cpumark_chart,diskmark_chart,g2dmark_chart,g3dmark_chart):'
bdurl = 'http://www.iphonebenchmark.net/' + str(raw_input(u'http://www.iphonebenchmark.net/')) +'.html'
values=iphonebenchmark_Spider(bdurl)
values.iphoneBenchMark()
print values.iphonebenchmark
iphone=BenchMark()
for var in values.iphonebenchmark:
iphone.Iphone(var)