使用Python写的第一个网络爬虫程序

今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中。


此代码中主要使用到了python的以下几个功能,因为对python不熟悉,把代码也粘贴在以下。


1, 使用url打开站点网页

import urllib2

data = urllib2.urlopen(string_full_link).read().decode('utf8')
print data

2,使用正則表達式匹配

import re

#一般的英文匹配
reg = """a href=S* target='_blank' title=S*"""
dicList = re.compile(reg).findall(data)
print dicList
#中文的正则匹配,须要使用中文相应的unicode码
reg=u"u5730u5740S*"      #“地址”相应的 unicode code
addrList = re.compile(reg).findall(sub_data)
print addrList

3,写数据到excel文件

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""")


原文地址:https://www.cnblogs.com/gccbuaa/p/7217680.html