python网络爬虫学习之路
来源:互联网 发布:小米笔记本 centos 编辑:程序博客网 时间:2024/04/29 16:06
安装检查网站构建的技术类型工具
pip install builtwith
敲 python 进入python
import builtwith
builtwith.parse('http://www.baidu.com')
安装寻找网站所有者的库
pip install python-whois
import whois
print whois.whois('www.baidu.com')
编写第一个简单的爬虫
#coding = utf-8
import urllib2
def download(url):
print 'downloading:', url
return urllib2.urlopen(url).read()
html = download('http://www.kuaizhaohuodi.com/')
print html
更健壮的版本
#coding = utf-8
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
html = download('http://www.kuaizhaohuodi.com/')
print html
重复下载版本
#coding = utf-8
import urllib2
def download(url, num_retries=2):
print 'downloading:',url
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print 'downloading erro:', 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
html = download('http://httpstat.us/500')
print html
设置用户代理
#coding = utf-8
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 'downloading error:', e.reason
html = None
if num_retrise > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
return download(url, user_agent, num_retries-1)
return html
html = download('http://www.imooc.com/')
print html
- python网络爬虫学习之路
- python学习之网络爬虫
- Python学习—网络爬虫之OJ
- 学习Python之网络爬虫(一)
- Python之网络爬虫学习笔记
- python 网络爬虫学习笔记之beautifulsoup
- python网络爬虫学习笔记之实力爬虫(
- python网络爬虫学习
- Python网络爬虫学习
- 网络爬虫学习 python
- Python之网络爬虫
- Python--之网络爬虫
- Python之网络爬虫
- python网络爬虫学习笔记之之正则表达式
- python网络爬虫学习笔记
- Python网络爬虫 学习资源----
- Python网络爬虫基础知识学习
- Python--网络爬虫学习笔记
- Python--元组的操作
- 动态代理解决中文乱码
- redis简介
- linux-c之函数(函数指针、函数传参、命令行参数)
- Kblack loves flag
- python网络爬虫学习之路
- 从Notification发通知跳转引发的几个问题
- 水平居中
- java 遍历map 方法 集合 五种的方法
- css3之动画效果animation
- [编程题]字符串运用-密码截取
- grub2 中注册自定义的命令
- Android数据持久化存储(一)
- 80. Remove Duplicates from Sorted Array II