#-*- coding: UTF-8 -*-
import urllib,urllib2,re,time
file = open('baidukey.txt','r')
content = file.read().split('/n')
file.close()
baidukey=[]
savefile=''
number =100
count = 0
for key in content:
url = 'http://www.baidu.com/s?wd=' + urllib2.quote(key)
print url
req = urllib2.Request(url)
raw = urllib2.urlopen(req)
text = raw.read()#获得搜素页面内容
rst = re.compile(r'nowrap class="f14"><a href="s.+?>(.+?)</a?>',re.U|re.I|re.S)
m = rst.findall(text)
if m :
print '抓取解析成功'
else:
print '解析'+url +'时有问题'
for kkey in m:
if kkey not in baidukey:
baidukey.append(kkey)
savefile = savefile +kkey +'/n'
count = count + 1
#print savefile
if count > number:
file = open('savefile.txt','a')
file.write(savefile)
file.close()
savefile=''
count=0
file = open('savefile.txt','a')
file.write(savefile)
file.close()
print '抓取结束。。。。'
'baidukey.txt'内容:
杭州电子科技大学