学习python写网络爬虫(一)
2016-09-16 23:27
561 查看
寻找网站所有者,可以使用WHOIS协议查看域名的注册者是谁。使用whois模块可以查看。
在linux在安装模块:pip install python-whois
在windows安装模块:
1. 下载模块并解压
2. 打开cmd,定位的解压模块目录
3. 运行命令:setup.py build
setup.py install
4. 重新打开python IDE, import 模块名称 ,没报错则安装成功
在linux在安装模块:pip install python-whois
在windows安装模块:
1. 下载模块并解压
2. 打开cmd,定位的解压模块目录
3. 运行命令:setup.py build
setup.py install
4. 重新打开python IDE, import 模块名称 ,没报错则安装成功
#最简单的爬虫 import urllib2 def download(url): return urllib2.urlopen(url).read() print download('http://www.cnblogs.com/guoyongheng')
#更加健壮的版本,可以捕获异常了 import urllib2 def download(url): print 'Downloading:',url try: html = urllib2.urlopen(url).read() except urllib2.URLError as e: print 'Download error:',e.reason html = None return html print download('http://www.cnblogs.com/guoyongheng')
#如果发生5xx类型的错误,可以重试下载 import urllib2 def download(url,num_retries = 2): print 'Downloading:',url try: html = urllib2.urlopen(url).read() except urllib2.URLError as e: print 'Download error:',e.reason html = None if num_retries > 0: if hasattr(e,'code') and 500 <= e.code < 600: return download(url,num_retries-1) return html print download('http://httpstat.us/500')
#为了下载更加可靠,设置了一个默认的用户代理“wswp” #与之前写的代码的对比就是加了代理之后,爬我的csdn博客时可以 #爬下来了,而不加代理的时候,则无法爬取 import urllib2 def download(url, user_agent = 'wswp', num_retries = 2): print 'Downloading:',url headers = {'User-agent':user_agent} request = urllib2.Request(url,headers=headers) try: html = urllib2.urlopen(request).read() except urllib2.URLError as e: print 'Download error:',e.reason html = None if num_retries > 0: if hasattr(e,'code') and 500 <= e.code < 600: return download(url,num_retries-1) return html print download('http://blog.csdn.net/gyhguoge01234')
相关文章推荐
- 使用python2.7学习网络爬虫,问题锦集
- [Python]网络爬虫学习笔记,爬取豆瓣妹子上妹子的照片
- Python网络爬虫基础知识学习
- Python爬虫学习(8):浙大软院网络登陆保持
- 【Python开发】【神经网络与深度学习】网络爬虫之图片自动下载器
- Python 基础学习 网络小爬虫
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- Python网络爬虫框架scrapy的学习
- Python 基于学习 网络小爬虫
- 学习python写网络爬虫(四)
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- 学习python写网络爬虫(二)
- 【爬了个爬——学习Python网络爬虫】1.抓取页面
- [Python]网络爬虫学习笔记,爬取东南大学SEU-wlan的流量使用情况
- Python学习笔记之网络爬虫
- [Python] 网络爬虫和正则表达式学习总结
- 【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现