Python抓取网页中的链接
来源:互联网 发布:外国人淘宝 编辑:程序博客网 时间:2024/04/28 03:15
需要从web中抓取相关的网页。正好想学习一下Python,首先看了一下Python简明教程,内容讲的不多,但是能够使你快速入门,我一直认为实例驱动学习是最有效的办法。所以直接通过实际操作怎么去抓取网页来丰富对Python的学习效果会更好。
Python提供了各种各样的库,使得各种操作变得很方便。这里使用的是Python的urllib2和sgmllib库。为了处理HTML,Python总共提供了三个模块:sgmllib htmllibHTMLParser。本文中采用的是sgmllib,但是通过查找相关资料发现其实第三方工具BeautifulSoup是最好的,能够处理较差的HTML。所以后面还要接着学习BeautifulSoup。
(2)脚本代码
import urllib2import sgmllibclass LinksParser(sgmllib.SGMLParser):urls = []def do_a(self, attrs):for name, value in attrs:if name == 'href' and value not in self.urls:if value.startswith('http'):self.urls.append(value)print valueelse:continuereturnif __name__ == "__main__":# str = ""# if str.strip() is '':# print "str is None"# else:# print "str is no None"p = LinksParser()f = urllib2.urlopen('http://www.baidu.com')value = f.read()print valuep.feed(value)for url in p.urls:print urlf.close()p.close()
转自:http://blog.csdn.net/cscmaker/article/details/8730153
- Python抓取网页中的链接
- python抓取网页中的链接地址
- Python抓取网页链接
- Python抓取网页链接
- 抓取网页中的链接
- 抓取网页中的链接
- 抓取网页中的链接
- PYTHON抓取网页中的邮箱
- Python抓取网页链接,存入mysql
- python爬虫抓取目标网页链接
- ASP.NET实现抓取网页中的链接
- ASP.NET实现抓取网页中的链接
- python抓取网页(一)--获得网页链接
- python 网页抓取中的中文乱码问题解决
- 用 python 抓取网页中的动态数据
- Python简单抓取新浪某网页新闻链接及标题
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- Learning Ruby:Features of Ruby
- debug in Myeclipse ClassNotFoundException
- 二叉树系列问题1
- MyBatis多对一,一对多关联映射
- unity基础开发----Unity3D的uniSWF插件动态加载SWF UI资源
- Python抓取网页中的链接
- Perl文件类型判断
- 善用Android预定义样式
- 求职简历撰写要点
- log file sync和log file parallel write的关系
- thinkphp二级域名配置
- 【干货:华为的股权、研发、治理】
- Python 获取接口数据,解析JSON,写入文件
- 【hdu3473】【划分树】Mininum Sum