通过urllib2抓取HTML网页,然后过滤出包含特定字符的行,并写入Excel文件:

# -*- coding: utf-8 -*-

import sys
#import urllib
import urllib2 from xlwt import Workbook def getdata(keywords, line):
date = ''
if keywords in line: # 本行包含keywords
start = line.find('>',)
end = line.find('</', start)
data = line[start+1:end]
return data
return False def FetchDataByUrllib(checkUrl):
book=Workbook(encoding='gbk')
# add_sheet新增sheet,默认不能overwrite数据,必须显示指定可更改。
sheet=book.add_sheet('mySheet', cell_overwrite_ok=True) try:
checkFile = urllib2.urlopen(checkUrl)
except Exception, e: print e
return type = sys.getfilesystemencoding() i = 1
for line in checkFile:
# 根据网页编码格式来解码
line = line.decode("UTF-8").encode(type)
#line = line.decode("GBK").encode(type) # 逐行全部写入excel文件。
#sheet.write(i,1,line)
#i+=1 # 查找所需的特定数据,写入Excel文件。
targetStr = getdata('体育', line) # 包含'体育'的行
if targetStr != False:
sheet.write(i,1,targetStr)
i+=1 book.save('simple.xls')
print 'finish!' print '开始...' myUrl = 'http://www.sina.com.cn' FetchDataByUrllib(myUrl)

输出结果:

urllib2抓取HTML存入Excel的更多相关文章

  1. python 抓取数据 存入 excel

    import requestsimport datetimefrom random import choicefrom time import timefrom openpyxl import loa ...

  2. 爬虫学习一系列:urllib2抓取网页内容

    爬虫学习一系列:urllib2抓取网页内容 所谓网页抓取,就是把URL地址中指定的网络资源从网络中读取出来,保存到本地.我们平时在浏览器中通过网址浏览网页,只不过我们看到的是解析过的页面效果,而通过程 ...

  3. python使用urllib2抓取网页

    1.使用python的库urllib2,用到urlopen和Request方法. 2.方法urlopen原形 urllib2.urlopen(url[, data][, timeout]) 其中: u ...

  4. 【Python开发】python使用urllib2抓取防爬取链接

    前几天刚看完<Linux/Unix设计思想>,真是一本不错的书,推荐想提高自己代码质量的童鞋看一下,里面经常提到要以小为美,一个程序做好一件事,短小精悍,因此我也按照这种思想来写pytho ...

  5. python2 urllib2抓取51job网的招聘数据

    #coding=utf-8 __author__ = "carry" import sys reload(sys) sys.setdefaultencoding('utf-8') ...

  6. 通过urllib2抓取网页内容(1)

    一.urllib2发送请求 import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urll ...

  7. python抓取历年特码开奖记录

    背景: 小时候,有种游戏,两个主人公:白XX和曾XX,每个家庭把他俩像活菩萨一样供着,供他们吃,供他们穿 做生意的老板为了这两位活菩萨,关门大吉 农民为了这两位活菩萨卖牛卖田变卖家产 做官的为了这两位 ...

  8. python 爬虫抓取心得

    quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...

  9. 使用python抓取知乎日报的API数据

    使用 urllib2 抓取数据时,最简单的方法是: import urllib2, json def getStartImage(): stream = urllib2.urlopen('http:/ ...

随机推荐

  1. SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException: xxx

    刚配置hadoop2.2,格式化namenode时候报的这个错. 原因是hadoop在格式化HDFS的时候,通过hostname命令获取到的主机名在/etc/hosts文件中进行映射的时候,没有找到, ...

  2. shell 中变量前"?"的作用

    example: if ($?USER == 0 || $?prompt == 0)   then       ....     if ( $?DS_HOME != 0 ) then if ( ${? ...

  3. ubuntu 下更新PHP版本 ZT

    sudo add-apt-repository ppa:ondrej/php5 如果报错 add-apt-repository: command not found add-apt-repositor ...

  4. C# 生成条形码图片,效果不错

    //首先引用 条码库BarcodeLib.dll using System; using System.Collections.Generic; using System.Linq; using Sy ...

  5. 富文本编辑器kindeditor配置

    <!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...

  6. github最简单的操作方法

    其实,说实话,到目前为止,我还没有研究透那些gitbush上面的命令,所以,往github上面上传自己的文件,我采用最简单的操作方式.嘻嘻.下面,将为大家讲述一下. 首先,要在github上面注册新用 ...

  7. CSS hack方式一览【转】

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  8. psp记录个人项目花费时间

    预估时间 120min 设计分析时间 30min 具体设计时间 ≍40min 编码时间 ≍1h 测试时间 10min 整理结论时间 10min 总结与探讨时间 5min

  9. linux文件系统节点详解

    linux文件系统有两层结构,逻辑结构和物理结构.也就是inode和block. 每个文件都有一个inode, 记录文件属性:权限,时间还有最重要的block号码. block是实际存放文件内容的地方 ...

  10. rails使用bootstrap3-wysiwyg可视化编辑器并实现自定义图片上传插入功能

    之前在rails开发中使用了ckeditor作为可视化编辑器,不过感觉ckeditor过于庞大,有很多不需要的功能,而且图片上传功能不好控制不同用户可以互相删除图片,感觉很不好.于是考虑更改可视化编辑 ...