urlib2 标准代码
import urllib2
def downloadHtml(url,user_agent=None,num_retries=2):
print 'Downloading:',url
headers={'User-agent':user_agent}
req=urllib2.Request(url,headers=headers)
try:
html=urllib2.urlopen(req).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 downloadHtml(url,user_agent,num_retries-1)
return html
def download_id():##根据连续页码下载若连续5次出错停止下载
max_count=5
error_count=0
for i in itertools.count(1):
url='http://xxxx/%s'%i
html=download(url)
if html is None:
error_count+=1
if error_count==max_count:
break
else:
error_count=0 def get_links(html):
reg=re.compile(r'',re.S)
return reg.findall(html) def link_crawler(seed_url, link_regex):
crawl_queue=[seed_url]
seen=set(crawl_queue)
while crawl_queue:
url=crawl_queue.pop()
html=download(url)
for link in get_links(html):
if re.match(link_regex,link):
link=urlparse.urljoin(seed_url,link)
if link not in seen:
seen.add(link)
crawl_queue.append(link)
urlib2 标准代码的更多相关文章
- 计算KS值的标准代码
计算KS值的标准代码 from scipy.stats import ks_2samp get_ks = lambda y_pred,y_true: ks_2samp(y_pred[y_true==1 ...
- 标准代码页(codepage)列表
https://blog.csdn.net/jianggujin/article/details/80325461 这篇文章有待完善 代码页 简称 全称 37 IBM037 IBM EBCDIC (U ...
- [模板] KMP字符串匹配标准代码
之前借鉴了某个模板的代码.我个人认为这份代码写得很好.值得一背. #include<bits/stdc++.h> using namespace std; const int N=1000 ...
- 标准代码书写 C++ 的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果 离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是 ...
- linux下定时网站文件备份和数据备份以及删除旧备份标准代码
直切正题: 文件备份:web.sh 数据备份:db.sh 删除旧备份:clear.sh vi web.sh文件内容为: #!/bin/bash 解释:shell脚本标准头 cd 网站文 ...
- css标准导航代码
<!-- 例子解析: --> --> <!-- list-style-type:none - 移除列表前小标志.一个导航栏并不需要列表标记 --> <!-- 移除浏 ...
- OAF_开发系列28_实现OAF中反编译获取class包代码JD Compiler(案例)
20150730 Created By BaoXinjian
- Java代码规范
Java代码规范 本Java代码规范以SUN的标准Java代码规范为基础,为适应我们公司的实际需要,可能会做一些修改.本文档中没有说明的地方,请参看SUN Java标准代码规范.如果两边有冲突,以SU ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
随机推荐
- 日志组件Log4Net
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...
- Javascript时间差计算函数代码实例
Javascript时间差计算函数代码实例 <script language="javascript"> Date.prototype.dateDiff = funct ...
- VS2013 生成sqlite3动态连接库及sqlite3.dll的调用
一,生成sqlite3动态连接库1,去sqlite官网上下载最近的sqlite源码包,解压后得到四个文件:shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h此处还需要sq ...
- JDK 8的依赖使用
第一步:compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VER ...
- ffmpeg muxer 参数简要说明
参数 值 说明 movflags MP4 Muxer 标记 rtphint 增加RTP的hint track empty_moov 初始化空的moov box frag_keyframe 在视频关键帧 ...
- WCF 小程序案例以及序列化的使用
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;u ...
- 关于boostrapValidator动态添加字段(addField)验证的bug
每次码博客,都觉得自己怀才不遇,哎~脑袋有瑕疵,文笔拿不粗手,就直接上干货吧. 在使用boostrapValidator这个验证插件的时候,如果某一个字段是动态添加来的,我们需要调用方法:addFie ...
- 使用bottle进行web开发(2):http request
我们知道,http request有多个方法,比如get,post,delete,patch,put等.对用的,bottle都定义了相应的装饰器,目前定义了五个: get(),post(),put() ...
- android launchmode singleinstance问题
问题描述 最近测试关于launchmode的四种方式 默认模式 top singletask 都已经了解了 唯独这个instance模式 我的问题是 我们只作2个activity的假设A和B,其中A为 ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...