今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中。
此代码中主要使用到了python的以下几个功能,因为对python不熟悉,把代码也粘贴在以下。
1, 使用url打开站点网页
import urllib2data = urllib2.urlopen(string_full_link).read().decode('utf8')
print data2,使用正則表達式匹配
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 codeaddrList = re.compile(reg).findall(sub_data)
print addrList3,写数据到excel文件
import xlrdimport 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""")