[Python]爬虫v0.1
2023-02-18 16:47:47 时间
#coding:utf-8 import urllib ###### #爬虫v0.1 利用urlib2 和 字符串内建函数 ###### # 获取网页内容 def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def content(html): # 内容分割的标签 str = '<article class="article-content">' content = html.partition(str)[2] str1 = '<div class="article-social">' content = content.partition(str1)[0] return content # 得到网页的内容 def title(content,beg = 0): # 思路是利用str.index()和序列的切片 try: title_list = [] while beg >=0: num1 = content.index('】',beg) num2 = content.index('</p>',num1) title_list.append(content[num1:num2]) beg = num2 except ValueError: return title_list def get_title(): # 利用循环更新num1和num2,从而匹配出全部title pass content = content(getHtml("http://bohaishibei.com/post/10449/")) #num = content.index('】') title = title(content) for i,e in enumerate(title): print '第%d个,title:%s' % (i,e) # 今天爬的单个页面的title
只是粗略的记录写爬虫的过程和思路,本来打算直播的,但是我们十一点断电断网。明天续写这个文章,直播写爬虫。哈哈哈,虽然基础,但是也是写出来吧。
相关文章
- 在PyQt中构建 Python 菜单栏、菜单和工具栏
- 结合scipy.linalg在Python中使用线性系统
- [Python] 函数基本
- [Python] 使用dict和set
- [Python] Python基础字符串
- Python入门:ChainMap 有效管理多个上下文
- 什么是MircoPython?
- [Python] 函数基本
- [Python] 使用dict和set
- [Python] Python基础字符串
- Python 绑定:从 Python 调用 C 或 C++
- Python 没有函数重载?如何用装饰器实现函数重载?
- [oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
- 这15个顶级Python库,你必须要试试!
- ikun必学!python 画一个简单的只因
- 技术实践:教你用Python搭建gRPC服务
- 带你掌握4种Python 排序算法
- 一文讲全了Python 类和对象内容
- 什么是Python中的套接字编程?
- [Python]介绍