import csv,re
def search(req,line):
text = re.search(req,line)
if text:
data = text.group(1)
else:
data = 'no'
return data
csvfile = file('serp_html.csv','rb')
reader = csv.reader(csvfile)
'''输出百度搜索结果数据:当前关键词,排名,排名网站,百度url(需转义后才是真实的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)
print '%s,%s,%s,%s,%s' % (word,number,domain,bdurl,title)
以上是一个继承程序,运行后能print出正确结果,但是我希望能生成csv报表文件,尝试修改for为函数失败。
小菜鸟一枚,不知道怎么搞了,求大神指点
It can be like this
The code has not been tested, please modify it simply if there are any problems