小白Python3爬虫
来源:互联网 发布:js获取input 编辑:程序博客网 时间:2024/05/20 15:38
python网络爬虫Ver 1.0 alpha
爬虫入口是feng的Startup News(http://news.dbanotes.net/)
程序如下:
import reimport urllib.requestimport urllibfrom collections import dequequeue=deque()#空队列visited=set()#空集合,用来存储访问过的网址url='http://news.dbanotes.net/news'#入口页面可以换成别的queue.append(url)#将入口网址放入队列当中cnt=0#循环直到队列不为空while queue: url=queue.popleft()#队首元素出队 visited|={url}#标记为已访问 print('已经抓取:'+str(cnt)+'正在抓取<---'+url) cnt+=1 urlop=urllib.request.urlopen(url)
#if 'html' not in urlop.gentheader('Content-Type'): # continue #避免程序异常中止,用try..catch处理异常 try: data=urlop.read() data = data.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)程序结果如图,我也不知道对不对,但总归是爬到一点东西,我就不深思了,反正我这个小白也不会懂。程序里面有两行被注释掉的程序是因为我自己跑不通,所以只能注释掉了
今天就暂时到这吧嘻嘻
转载于https://jecvay.com/2014/09/python3-web-bug-series2.html
阅读全文
0 0
- 小白Python3爬虫
- 小白自学Python3爬虫
- 小白自学Python3爬虫
- 小白Python3爬虫3-5
- python3.4-小爬虫
- python3.3 爬虫小例子
- python3.4-小爬虫2
- 小白Python3爬虫—HTTP(二)
- python3.2 多线程小爬虫一只
- python3爬虫简单小实例1.0
- python3爬虫简单小实例2.0
- 小白Python3爬虫—简介www和http(一)
- python3 爬虫
- python3爬虫
- python3 爬虫
- Python3 爬虫
- python3.5小爬虫—爬糗事百科
- Python3批量爬取网页图片(小爬虫)
- 高德地图计算两点之间的距离java+html
- Myeclipse 自动添加注释
- Linux下安装和卸载补丁包
- Qt qDebug() 的使用方法
- 科技创新才能引领未来
- 小白Python3爬虫
- LSP剩余时间(Remaining Lifetime)
- Java
- Error:All flavors must now belong to a named flavor dimension. The flavor 'flavor_name' is ...
- 80. Remove Duplicates from Sorted Array II
- Minimum Moves to Equal Array Elements II
- 怎样为爱犬挑选合适的狗粮
- linux清除文件内容最快的方法
- Unity 保存图片文理