今天尝试使用python写一个网络爬虫代码,主要是想访问某个网站,从中选取感兴趣的信息,并将信息按照一定的格式保存早Excel中。
此代码中主要使用到了python的以下几个功能,由于对python不熟悉,把代码也粘贴在下面。
1, 使用url打开网站网页
import urllib2
data = urllib2.urlopen(string_full_link).read().decode('utf8')print data
import re #一般的英文匹配 reg = """a href=\S* target='_blank' title=\S*""" dicList = re.compile(reg).findall(data) print dicList
#中文的正则匹配,需要使用中文对应的unicode码 reg=u"\u5730\u5740\S*" #“地址”对应的 unicode code addrList = re.compile(reg).findall(sub_data)
print addrList
import xlrd
import xlwt
file = xlwt.Workbook()
table = file.add_sheet('hk', cell_overwrite_ok=True)
print index, name, addr, tel
table.write(index, 0, name)
table.write(index, 1, addr)
table.write(index, 2, tel)
file.save("""D:\\test.xls""")原文地址:http://blog.csdn.net/wlqingwei/article/details/46331809