python简单爬虫爬取队列的实现
2016-07-26 21:29
609 查看
deque()是双端队列。set()中不允许出现重复的内容。 import re import urllib import urllib.request from collections import deque queue=deque()#待抓取的网页 visited=set()#已经抓取过的网页 url='https://www.zhihu.com/topic/19570752/top-answers' queue.append(url)#将链接放入待访问队列 num=0 while queue: url=queue.popleft()#从左侧取出一个链接 visited.add(url) num=num+1 print('正在抓取第',num,'条链接') urlop=urllib.request.urlopen(url) if 'html' not in urlop.getheader('Content-Type'):#不是html页面 # continue try: data=urlop.read().decode('utf-8') except: continue linkre=re.compile('href=\"(.+?)\"')#利用正则表达式,取出所有链接 for x in linkre.findall(data):#遍历上面的链接 集合 if 'http' in x and x not in visited: queue.append(x) print(x,'加入队列')
相关文章推荐
- python抓取知乎首页文本信息的简单实现
- 6. ZigZag Conversion [easy] (Python)
- Python 第十课,面向对象补足,异常处理
- Python 第九课,面向对象进阶
- 【Python】map、filter、reduce函数简介
- Python 培训之MySql
- python循环与判断
- [Python] 技术贴系列之——Django部署方案简介
- 【Python】基础学习——列表(list),元祖(tuple),字典(dict),字符串(string),集合(set)
- 1. Two Sum [easy] (Python)
- Ubuntu下Python3和Python2的共存
- python不能安装库文件的解决方法
- 374. Guess Number Higher or Lower [easy] (Python)
- python的一个好玩模块wordcloud
- 彻底解决安装Python扩展包时Unable to find vcvarsall.bat
- Python中的序列相机和相乘
- python-argparse
- 关于python netsnmp模块 snmpwalk工具 timeout参数说明
- python学习
- 用 Python 写了个简单的股票量化交易框架