python抓取
2024-10-20 20:47:39
我要抓取奥巴马每周的演讲内容http://www.putclub.com/html/radio/VOA/presidentspeech/index.html
如果手动提取,就需要一个个点进去,再复制保存,非常麻烦。
那有没有一步到位的方法呢,用python这种强大的语言就能快速实现。
首先我们看看这网页的源码
可以发现,我们要的信息就在这样一小条url中。
更具体点说,就是我们要遍历每个类似http://www.putclub.com/html/radio/VOA/presidentspeech/2014/0928/91326.html这样的网址,而这网址需要从上面的网页中提取。
好,开始写代码
首先打开这个目录页,保存在content
- import sys,urllib
- url="http://www.putclub.com/html/radio/VOA/presidentspeech/index.html"
- wp = urllib.urlopen(url)
- print "start download..."
- content = wp.read()
下面要提取出每一篇演讲的内容
具体思路是搜索“center_box”之后,每个“href=”和“target”之间的内容。为什么是这两个之间,请看网页源码。
得到的就是每一篇的url,再在前面加上www.putclub.com就是每一篇文章的网址啦
- print content.count("center_box")
- index = content.find("center_box")
- content=content[content.find("center_box")+1:]
- content=content[content.find("href=")+7:content.find("target")-2]
- filename = content
- url ="http://www.putclub.com/"+content
- print content
- print url
- wp = urllib.urlopen(url)
- print "start download..."
- content = wp.read()
有了文章内容的url后,同样的方法筛选内容。
- #print content
- print content.count("<div class=\"content\"")
- #content = content[content.find("<div class=\"content\""):]
- content = content[content.find("<!--info end------->"):]
- content = content[:content.find("<div class=\"dede_pages\"")-1]
- filename = filename[filename.find("presidentspeech")+len("presidentspeech/"):]
最后再保存并打印
- filename = filename.replace('/',"-",filename.count("/"))
- fp = open(filename,"w+")
- fp.write(content)
- fp.close()
- print content
OK,大功告成!保存成.pyw文件,以后只需双击就直接保存下了obama每周演讲内容~
最新文章
- nodejs创建http服务器
- js callee,caller学习
- iOS 系统根据导航栏和状态栏自动修改布局
- iOS应用内跳转系统设置相关界面的方法
- response与文件下载
- QUnit使用笔记-5简化编写
- Anddoi 将时间转换为指定时区的时间
- linux ssh scp无密码登录
- 5.7.13mysql 无法登陆
- 在FL2440上使用kei MDK 调试程序(J-link)
- Google日历添加农历、节日和天气插件(步骤)
- graph driver-device mapper-04libdevmapper基本操作
- Qt 打包发布 不能动态打开图片显示问题
- [转]layui点击左侧导航栏,实现不刷新整个页面,只刷新局部
- opencv 图片缩放
- IDEA对新建java线程池的建议
- 约束Constraints--主键约束、外键约束、唯一约束、检查约束、默认约束、NOT NULL约束、列约束与表约束、创建约束、删除约束
- window10总提示幸福倒计时,解决方法
- 清空select标签中option选项的3种不同方式
- python - 配置文件
热门文章
- xml配置文件特殊符号的处理方法
- 使用sass与compass合并雪碧图(二)
- Daily Srum 10.26
- Daily Scrumming 2015.10.22(Day 3)
- Chapter 3 软件项目管理
- Hibernate笔记④--一级二级缓存、N+1问题、saveorupdate、实例代码
- POJ 2096 Collecting Bugs 期望dp
- apache 2.4目录权限
- [51单片机] Keil C51中变量的使用方法详解
- 深入浅析JavaScript的API设计原则(转载)