urllib2抓取HTML存入Excel
通过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的更多相关文章
- python 抓取数据 存入 excel
import requestsimport datetimefrom random import choicefrom time import timefrom openpyxl import loa ...
- 爬虫学习一系列:urllib2抓取网页内容
爬虫学习一系列:urllib2抓取网页内容 所谓网页抓取,就是把URL地址中指定的网络资源从网络中读取出来,保存到本地.我们平时在浏览器中通过网址浏览网页,只不过我们看到的是解析过的页面效果,而通过程 ...
- python使用urllib2抓取网页
1.使用python的库urllib2,用到urlopen和Request方法. 2.方法urlopen原形 urllib2.urlopen(url[, data][, timeout]) 其中: u ...
- 【Python开发】python使用urllib2抓取防爬取链接
前几天刚看完<Linux/Unix设计思想>,真是一本不错的书,推荐想提高自己代码质量的童鞋看一下,里面经常提到要以小为美,一个程序做好一件事,短小精悍,因此我也按照这种思想来写pytho ...
- python2 urllib2抓取51job网的招聘数据
#coding=utf-8 __author__ = "carry" import sys reload(sys) sys.setdefaultencoding('utf-8') ...
- 通过urllib2抓取网页内容(1)
一.urllib2发送请求 import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urll ...
- python抓取历年特码开奖记录
背景: 小时候,有种游戏,两个主人公:白XX和曾XX,每个家庭把他俩像活菩萨一样供着,供他们吃,供他们穿 做生意的老板为了这两位活菩萨,关门大吉 农民为了这两位活菩萨卖牛卖田变卖家产 做官的为了这两位 ...
- python 爬虫抓取心得
quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...
- 使用python抓取知乎日报的API数据
使用 urllib2 抓取数据时,最简单的方法是: import urllib2, json def getStartImage(): stream = urllib2.urlopen('http:/ ...
随机推荐
- jython安装
下载了jython后:http://www.cr173.com/soft/9719.html 这是我当时下载的网站 官网很慢开始->运行->cmd->打开dos命令窗口,转到jyth ...
- linux命令:xargs
1.命令介绍: xargs用来配合find命令查找的结果然后执行相应的命令 2.命令格式: find -type f -print | xargs file
- SpringMvc+Spring+Mybatis的jar包依赖关系图
- PHP explode()函数
源起:将日期格式的字符串拆分成年.月.日,用于组织关系介绍信的特定位置打印.感谢倪同学提供思路 定义和用法 explode()函数把字符串分割为数组 语法 explode(separator,stri ...
- Android SDK ADT下载地址
http://dl.google.com/android/android-sdk_rXX-windows.zip http://dl.google.com/android/ADT-X.X.X.zip ...
- ios网络知识
http://www.cocoachina.com/bbs/read.php?tid-31300.html 启蒙内容 http://blog.csdn.net/nono_love_lilith/ar ...
- 修复Windows XP右键没有新建菜单问题
桌边上,点击鼠标右键,也没有排列图标菜单 开始-运行-输入:cmd输入命令:reg add "HKEY_CLASSES_ROOT\Directory\Background\shellex\C ...
- html5原生canvas内image旋转
目前理解下来就是旋转的不是image本身,而是要drawImage的那个canvas的2d context,context本身的绘制就是把图片本来的样子draw出来,至于旋转,透明度之类的效果都是对c ...
- linux grep,sed,awk和diff的使用
1:grep//显示行 # grep 'main' /home/myhome/a.c//将a.c含有main的行显示出来 # grep -v 'main' /home/myhome/a.c //显示除 ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...