可以这样
import csv,re
def search(req,line):
text = re.search(req,line)
if text:
data = text.group(1)
else:
data = 'no'
return data
reuslts = []
result_csv = file('new_file.csv', 'wb')
result_csv_writer = csv.writer(result_csv)
'''输出百度搜索结果数据:当前关键词,排名,排名网站,百度url(需转义后才是真实的url),标题'''
# 保存标题
result_csv_writer.writerow(['关键词', '排名', '排名网站', '百度url', '标题'])
for line in reader:
word = line[0]
html = line[1]
number = search(r'id="(d+)"',html)
domain = search(r'<span class="g">(.*?)/.*</span>',html)
bdurl = search(r'href="(http://www.baidu.com/link?url=[^"]*?)"',html)
title = search(r'"title":"([^"]*?)"',html)
reuslts.append((word, number, domain, bdurl, title))
# print '%s,%s,%s,%s,%s' % (word,number,domain,bdurl,title)
# 保存多行
result_csv_writer.writerows(reuslts)
result_csv.close()
代码未测试,有问题请简单修改
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…