我要搬家到csdn,大家到那里来看我吧,平台更大,看到的人更多!
ruby代码
#encoding: utf-8 require 'net/http' require 'open-uri' require 'nokogiri' # 用于解析html的模块 # sudo apt-get install libxslt-dev libxml2-dev # sudo gem install nokogiri require 'pathname' class JanDanSpider attr_accessor :base_uri, :cur_page # 定义属性访问器 def initialize(pagesize) @base_uri = 'http://jandan.net/pic' # @表示实例变量、@@表示类变量、$表示全局变量 @dir = '/media/Develop/MyCode/SevenLang/ruby/pic' @pagesize = Integer(pagesize) # 整型转换 end def crawl() Dir.mkdir @dir unless File.directory? @dir # 表判断的方法结尾都有个? totalpage = crawlpage(0) puts "pagesize #{totalpage}" # ""字符会引发字符串替换,''则不会 (1..@pagesize-1).each do |i| # 遍历元组 crawlpage(totalpage - i) end puts 'complete!' end def crawlpage(page) url = page==0 ? @base_uri : @base_uri+'/page-'+page.to_s # to_s是必要的 puts "crawl-page: #{url}" fpage = open(url) html = fpage.read doc = Nokogiri::HTML(html) doc.css('ol.commentlist li').each { |comment| match = /comment-(\d+)/.match(comment['id']) if match id = match[1] oo = Integer(comment.css('#cos_support'+'-'+id)[0].content); xx = Integer(comment.css('#cos_unsupport'+'-'+id)[0].content); xx = 1 if xx==0 if(oo>xx && (oo>200 || oo/xx >10) ) src = comment.css('p img')[0]["src"] puts "crawl: oo #{oo} xx #{xx} src #{src}" save_pic(src) end end } if page==0 cur_page = doc.css(".current-comment-page")[0].content page = Integer(/\d+/.match(cur_page).to_s) end puts "page #{page} done!" page # ruby中每条语句都有返回值,函数内最后一条语句的返回值会被return end def save_pic(url) urlpath = Pathname.new(url) filename = urlpath.basename.to_s dirpath = Pathname.new(@dir) filepath = dirpath.join(filename).to_s open(url) { |fin| open(filepath,"wb") { |fout| while buf = fin.read(4096) do fout.write buf end } } unless File.exists? filepath # 仅当文件不存在时进行抓取 puts 'done!' end end if ARGV.length == 1 spider = JanDanSpider.new($*[0]) # 可从 ARGV 或 $* 读取命令行参数 spider.crawl() else puts 'please input pagesize' #tip: puts, 转义+换行符 print, 转义 p, 换行 end
python
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #python-string 2 #python中的字符串用单引号''和双引号""标示 3 strA = 'this is a string' 4 strB = "this is a message!" 5 6 #打印两个字符串 7 print("打印两个字符串") 8 print('strA = ' + strA) 9 print('strB = ' + strB) 10 11 print("#############################") 12 13 strC = 'I don\'t know anything' 14 strD = '\'Yes\',I know.' 15 16 print("字符串中的转移字符") 17 print('strA = ' + strC) 18 print('strB = ' + strD) 19 20 print("#############################") 21 22 strE = '这是我的blog,欢迎大家来\n我的博客园溜达' 23 24 print("字符串中的换行") 25 print('strA = ' + strE) 26 27 print("#############################") 28 29 strF = 'this is ''message' 30 strG = 'Hongten' 31 strH = strG * 3 32 33 print('字符串可以用\'+\'号连接(或者说粘合),也可以用\'*\'号循环') 34 print('strF原有形式为:\'this is \'\'message\'') 35 print('粘合后的strF:' + strF) 36 print('strG原值为:\'Hongten\',strH = strG * 3,此时strH为:' + strH) 37 38 print("#############################") 39 strI = 'hongtenzone@foxmail.com' 40 41 print('字符串可以使用下标(索引)查询') 42 print('源字符串strI = \'hongtenzone@foxmail.com\'') 43 print('字符串strI的长度,len(strI) = ') 44 print(len(strI)) 45 print('strI[0] = ' + strI[0]) 46 print('strI[10] = ' + strI[10]) 47 print('strI[-1] = strI[len(strI) - 1]') 48 print('strI[-1] = ' + strI[-1]) 49 print('strI[len(strI) - 1] = ' + strI[len(strI) - 1]) 50 51 print("#############################") 52 53 print('Python 字符串不能改写。按字符串索引赋值会产生错误:') 54 print('strI[0] = \'x\',这样就会产生错误啦') 55 56 print("#############################") 57 58 print('过大的索引代替为字符串大小,下界比上界大的返回空字符串') 59 print('strI[0:100] = ' + strI[0:100]) 60 61 print("#############################") 62 63 print('索引可以是负数,计数从右边开始') 64 print('strI[-2] = ' + strI[-2]) 65 print('strI[-23:] = ' + strI[-23:]) 66 67 print("#############################") 68 69 print('不过-0 还是0,所以它不是从右边计数的!') 70 print('strI[0] = ' + strI[0]) 71 print('strI[-0] = ' + strI[-0])
java
/* * PROXY_HOST:代理的IP地址 * PROXY_PORT_INT:代理的端口号 */ Proxy proxy=newProxy(Proxy.Type.HTTP, newInetSocketAddress(PROXY_HOST, PROXY_PORT_INT)); URL url=newURL(urlStr); URLConnection conn=url.openConnection(proxy); InputStream is=conn.getInputStream();
html5js
/** * 这里只实现垂直滚动 */ var parent = document.getElementById('parent'); var content = document.getElementById('content') var startY = 0; // 初始位置 var lastY = 0; // 上一次位置 parent.addEventListener('touchstart', function(e) { lastY = startY = e.touches[0].pageY; }); parent.addEventListener('touchmove', function(e) { var nowY = e.touches[0].pageY; var moveY = nowY - lastY; var contentTop = content.style.top.replace('px', ''); // 设置top值移动content content.style.top = (parseInt(contentTop) + moveY) + 'px'; lastY = nowY; }); parent.addEventListener('touchend', function(e) { // do touchend var nowY = e.touches[0].pageY; var moveY = nowY - lastY; var contentTop = content.style.top.replace('px', ''); // 设置top值移动content content.style.top = (parseInt(contentTop) + moveY) + 'px'; lastY = nowY; });
/**
* 这里只实现垂直滚动
*/
var parent = document.getElementById('parent');
var content = document.getElementById('content')
var startY = 0; // 初始位置
var lastY = 0; // 上一次位置
parent.addEventListener('touchstart', function(e) {
lastY = startY = e.touches[0].pageY;
});
parent.addEventListener('touchmove', function(e) {
var nowY = e.touches[0].pageY;
var moveY = nowY - lastY;
var contentTop = content.style.top.replace('px', '');
// 设置top值移动content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
});
parent.addEventListener('touchend', function(e) {
// do touchend
var nowY = e.touches[0].pageY;
var moveY = nowY - lastY;
var contentTop = content.style.top.replace('px', '');
// 设置top值移动content
content.style.top = (parseInt(contentTop) + moveY) + 'px';
lastY = nowY;
});