python抓取网络内容
2015-09-16 13:26
525 查看
最近想做研究互联网来获取数据,只是有一点python,让我们来看一个比较简单的实现。
例如,我想抓住奥巴马的每周演讲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
详细思路是搜索“center_box”之后,每一个“href=”和“target”之间的内容。
为什么是这两个之间,请看网页源代码。
得到的就是每一篇的url,再在前面加上www.putclub.com就是每一篇文章的网址啦
OK,大功告成!保存成.pyw文件,以后仅仅需双击就直然后存储在obama每周简报~
例如,我想抓住奥巴马的每周演讲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每周简报~
相关文章推荐
- iOS开发网络篇—数据缓存
- 网络游戏中使用促销码
- AFNetworking从指定网址下载文件的两种方式总结 - 家柱
- HTTP 协议详解
- android 第二更(后台判断网络状态,反馈到页面)
- HTTPS原理详解
- 【网络】(十二)UDP简介
- WebHttpRequest Post请求
- Python网络爬虫 - 2. Beautiful Soup小试牛刀
- Linux网络配置及远程管理
- liferay 6.2 +httpclient4.5 调用 REST service
- Python网络爬虫 - 1. 准备工作
- 递归神经网络不可思议的有效性
- http状态码
- 网络IO之阻塞、非阻塞、同步、异步总结
- CCF 201403-4 无线网络 (二维最短路)
- Reachability 检测网络连接
- magento学习网址http://www.ruiwant.com/
- MPTCP 介绍
- iOS网络传输Delegate不被触发的本质原因