python 实现简单网络爬虫
2015-05-03 19:22
627 查看
python 实现简单网络爬虫
python 实现简单网络爬虫功能描述
链接文件的格式
Source Code
运行
这个学期一直在忙着做毕业设计,一直懒得写blog。想想还是记录一下为好。
功能描述:
1.分析链接文件,获取URL集合2.获取URL对应网页,存储到本地特定文件夹
链接文件的格式:
每一行两个URL 类似:URL1 URL2中间以
tab键隔开。
Note:这样的格式是因为我使用的是Sogou实验室提供的数据,用来实现PageRank算法的,这种文件格式方便PageRank算法实现。你可以在here下载
Source Code
import urllib import re import os import traceback #下载url对应的html def downLoadHtml(url): page = urllib.urlopen(url) html = page.read() return html #SogouT-Link.mini为链接文件 fp = open('F:\\WebCrawler\\SogouT-Link.mini','r') #创建url集合,集合中元素唯一,确保不重复下载网页 urlset = set() lines = fp.readlines() fp.close() for line in lines: urls = line.split('\t') urlset.add(urls[0]) urlset.add(urls[1]) for url in urlset: print '[+]',url id = 0 errCount = 0; for url in urlset: try: f = open('F:\\WebCrawler\\data\\'+'%s.html'%id,mode='w') print ('[+]start download %ss page\r\n' %id) html = downLoadHtml(url) f.write(html) id = id + 1 except Exception as e: print e errCount += 1 finally: f.close()
运行
$python Crawler.py
图示:
下载文件:
相关文章推荐
- Python实现简单网络爬虫功能
- python实现简单网络爬虫
- Python实现简单网络爬虫--转自极客头条
- 简单的网络爬虫的python实现
- python3.6 urllib.request库实现简单的网络爬虫、下载图片
- 5行python代码实现简单的网络爬虫
- Python实现简单的网络爬虫
- Python3.4编程实现简单抓取爬虫功能示例
- 训练神经网络的简单例子(TensorFlow平台下Python实现)
- python实现简单爬虫功能
- Python:入门到实现网络爬虫 Day1
- Python:入门到实现网络爬虫 Day4 --正则表达式
- Selenium + PhantomJS + python 简单实现爬虫的功能
- 02. 使用上述01安装库实现最简单的网络爬虫
- Python实现简单爬虫逻辑
- Python采集案例:Python实现爬取知乎神回复简单爬虫代码
- 用Python3实现一个简单的爬虫。
- Python3实现简单爬虫
- python实现最简单循环神经网络(RNNs)
- python实现简单爬虫功能