現在很多網站上某些活動都有限制同一IP只能投一票的規定,但是有時候迫于壓迫,又不得不想辦法多投幾票,以前是采用Apache里的HttpClient來實現這些功能,日前正在看Ruby,就用它也來玩下:
require 'net/http'
##獲得網頁內容
def query_url(url)
return Net::HTTP.get(URI.parse(url));
end
#抓取cnproxy上所有的代理列表,并將結果保存到proxy.txt中去
#你可以修改這塊代碼或者其他的代理服務器列表
def find_all_proxy
z="3";j="4";r="2";l="9";c="0";x="5";i="7";a="6";p="8";s="1"
pf = File.new("proxy.txt","w+")
for page_no in 1..10
url = "http://www.cnproxy.com/proxy#{page_no}.html"
content = query_url(url)
#print content
## ^$?./\[]{}()+*
for array in content.scan(/<td>(.*?)<SCRIPT type=text\/javascript>document.write\(":"\+(.*?)\)<\/SCRIPT><\/td>/)
if array.length == 2
pf.write("#{array[0]}:#{eval(array[1])}\n")
end
end
end
pf.close
end
##處理請求
def open_url_with_proxy(url)
pf = File.open("proxy.txt","r")
d = []
pf.each { |line| d << line }
for var in d
print "User Proxy #{var}\n"
begin
proxy = Net::HTTP::Proxy(var.split(":")[0],var.split(":")[1].to_i)
print proxy.get(URI.parse(url));
#print proxy.start("www.google.com",80){|http|
# response = http.get('/index.html')
# puts response.body
#}
rescue
##吃掉異常
end
end
end
##主程序
begin
if !FileTest.exist?( "proxy.txt" )
find_all_proxy
end
open_url_with_proxy('http://www.google.com/index.html');
end
這里需要注意的是代理服務器的端口不能是String類型,Ruby竟然不會自動轉換,搞得我浪費了N多時間.
最近終于空下來了,所以下個Ruby玩玩,安裝Ruby很簡單,去
官網下載一個
一鍵安裝包既可,linux下的安裝,大家Google下就有很多教程了.對于IDE網上說NetBeans支持得很完美,但是因為本人比較喜歡Eclipse,所以還是跟大家推薦
EasyEclipse for Ruby and Rails,當然你可以選擇只下RoR的插件而不弄個全新的Eclipse.
以前一直在用Java寫爬蟲工具抓圖片,對HttpClient包裝,正則表達式處理那個是累啊,就算弄好了工具類,有時候一會又想不起來放哪兒,但Ruby對方面包裝的就很強大,短短幾十行代碼就搞定了這一切:
頁面獲取和文件下載的方法.
util.rb:
require 'net/http'
def query_url(url)
return Net::HTTP.get(URI.parse(url));
end
def save_url(url,dir,filename)
filename = url[url.rindex('/')+1, url.length-1] if filename == nil || filename.empty?
require 'open-uri'
Dir.mkdir("#{dir}") if dir != nil && !dir.empty? && !FileTest.exist?(dir)
open(url) do |fin|
if true
File.new("#{dir}#{filename}","wb").close
open("#{dir}#{filename}","wb") do |fout|
while buf = fin.read(1024) do
fout.write buf
STDOUT.flush
end
end
end
end
end
抓取圖片的具體應用:
require "util"
begin
start_url = 'http://list.mall.taobao.com/1424/g-d-----40-0--1424.htm'
while start_url != nil && !start_url.empty? do
print "開始下載#{start_url}\n"
content = query_url(start_url)
next_page = content.scan(/ <a href="(.*?)" class="next-page"><span>下一頁<\/span><\/a>/)
next_url = nil
next_url = next_page[0][0] if next_page != nil && next_page.length > 0 && next_page[0].length > 0
imgs = content.scan(/<img src="(http:\/\/img[\d].*?)" \/>/)
for img in imgs
url = img[0];
save_url(url,"d:\\mall\\",nil)
end
start_url = next_url;
# break;
end
end
使用一天之后感覺ruby的語法很自然,很好理解,上手比較容易,而且相關包封裝的也很好,確實比較適合拿來玩玩小程序.
在開發中時常會遇到這樣的需求:讓某些描述信息(這些描述信息已經進行過安全html過濾,所以不會包含Javascript等腳本語言,但是允許正常的鏈接)里的鏈接失效,但是不要或者這些描述信息.如要以下代碼塊里的鏈接失效
<div id="desc">
<a href="http://www.9i56.cn">無聊網</a>
</div>
只需要再后面插入下段Javascript既可
<script type="text/javascript">
var elements = document.getElementById('desc').getElementsByTagName('A');
for (var i = 0, len = elements.length; i < len; ++i) {
elements[i].onclick = function(){return false;};
elements[i].href = "#";
}
var elementsArea = document.getElementById('desc').getElementsByTagName('area');
for (var i = 0, len = elementsArea.length; i < len; ++i) {
elementsArea[i].onclick = function(){return false;};
elementsArea[i].href = "#";
}
</script>
目前只知道a和area標簽可以放href屬性來進行跳轉,不知道大家還知道有其它的方式可以用href跳轉嗎?
日常工作中,常常要將在公司做的東西拷回家,或者要從家里拷東西到公司,但是如果用U盤拷又太麻煩,上web發郵件又有點煩,所以就做了下面的小程序,發送前切版里的內容到指定郵箱來傳遞文件.
相關技術點:
1.JMail郵件發送
2.剪切板提取
具體代碼實現如下:
/*
* Created on 2008-3-5
*/
package org.dueam.ft;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import sun.misc.BASE64Encoder;
/**
* 剪切板內容發生
* @author <a href="mailto:windonly@gmail.com">Anemone</a>
* hz,zj,china(2008-3-5)
*/
public class ClipboardFileTransmission {
/**
* @param args
* @throws EmailException
* @throws IOException
* @throws UnsupportedFlavorException
* @throws HeadlessException
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws EmailException, HeadlessException, UnsupportedFlavorException,
IOException {
String context = null;
List<File> fileList = null;
/**
* 處理前切版
*/
for (DataFlavor df : Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors()) {
//如果拷貝的是文本內容
if (df.equals(DataFlavor.stringFlavor)) {
context = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
else if (df.equals(DataFlavor.javaFileListFlavor)) {
//如果拷貝的是文件則當附件發送
fileList = (List<File>) Toolkit.getDefaultToolkit().getSystemClipboard().getData(
DataFlavor.javaFileListFlavor);
}
}
if ((null == context || "".equals(context)) && (fileList == null || fileList.isEmpty())) {
return;
}
if (null == context || "".equals(context)) {
context = "具體資料請看附件";
}
MultiPartEmail email = new MultiPartEmail();
// 發送服務器
email.setHostName("smtp.163.com");
//服務器用戶和密碼(如果你自己搞了臺不用驗證的郵件服務器就不用了)
email.setAuthentication("XXX", "XXX");
//接收的郵箱
email.addTo("XXX@gmail.com", "我的資料庫");
//發送服務器的郵件地址,現在很多郵件提供商都有驗證這個同用戶名是否對應,還是老老實實填真實的吧
email.setFrom("XXX@163.com", "Anemone");
email.setSubject("[日常資料傳遞]-" + getTime());
//文本編碼
email.setCharset("utf-8");
email.setMsg(context);
if (null != fileList)
for (File f : fileList) {
if (f.exists() && f.isFile()) {
//處理附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(f.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(getTime() + "By Anemone");
BASE64Encoder enc = new BASE64Encoder();
//附件中文名問題
attachment.setName("=?GBK?B?" + enc.encode(f.getName().getBytes()) + "?=");
email.attach(attachment);
}
}
email.send();
}
public static String getTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return df.format(new Date());
}
}
以上代碼在163和gmail之間測試通過過,建議用exe4j打成EXE文件,然后扔到system32目錄下面,這樣只要想發送資料的時候,只要復制下資料,再執行下這個命令就一切都OK了.
相關類包:
下載
ArrayUtils 擁有以下方法:
- toString
- 將一個數組轉換成String,用于打印數組
- isEquals
- 判斷兩個數組是否相等,采用EqualsBuilder進行判斷
- toMap
- 將一個數組轉換成Map,如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
- clone
- 拷貝數組
- subarray
- 截取子數組
- isSameLength
- 判斷兩個數組長度是否相等
- getLength
- 獲得數組的長度
- isSameType
- 判段兩個數組的類型是否相同
- reverse
- 數組反轉
- indexOf
- 查詢某個Object在數組中的位置,可以指定起始搜索位置
- lastIndexOf
- 反向查詢某個Object在數組中的位置,可以指定起始搜索位置
- contains
- 查詢某個Object是否在數組中
- toObject
- 將基本數據類型轉換成外包型數據
- isEmpty
- 判斷數組是否為空(null和length=0的時候都為空)
- addAll
- 合并兩個數組
- add
- 添加一個數據到數組
- remove
- 刪除數組中某個位置上的數據
- removeElement
- 刪除數組中某個對象(從正序開始搜索,刪除第一個)
eg:
// 1.打印數組
ArrayUtils.toString(new int[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(null, "I'm nothing!");// I'm nothing!
// 2.判斷兩個數組是否相等,采用EqualsBuilder進行判斷
// 只有當兩個數組的數據類型,長度,數值順序都相同的時候,該方法才會返回True
// 2.1 兩個數組完全相同
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 數據類型以及長度相同,但各個Index上的數據不是一一對應
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 數組的長度不一致
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 不同的數據類型
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new long[] { 1, 2, 3 });// false
ArrayUtils.isEquals(new Object[] { 1, 2, 3 }, new Object[] { 1, (long) 2, 3 });// false
// 2.5 Null處理,如果輸入的兩個數組都為null時候則返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true
// 3.將一個數組轉換成Map
// 如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
// 對于Object[]數組里的元素必須是instanceof Object[]或者Entry,即不支持基本數據類型數組
// 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})會出異常
ArrayUtils.toMap(new Object[] { new Object[] { 1, 2 }, new Object[] { 3, 4 } });// {1=2,
// 3=4}
ArrayUtils.toMap(new Integer[][] { new Integer[] { 1, 2 }, new Integer[] { 3, 4 } });// {1=2,
// 3=4}
// 4.拷貝數組
ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}
// 5.截取數組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4);// {1,5}
// 起始index為2(即第三個數據)結束index為4的數組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10);// {1,5,6}
// 如果endIndex大于數組的長度,則取beginIndex之后的所有數據
// 6.判斷兩個數組的長度是否相等
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
// 7.獲得數組的長度
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
// 8.判段兩個數組的類型是否相同
ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 });// true
ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 });// false
// 9.數組反轉
int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array);// {5,2,1}
// 10.查詢某個Object在數組中的位置,可以指定起始搜索位置,找不到返回-1
// 10.1 從正序開始搜索,搜到就返回當前的index否則返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
// 10.2 從逆序開始搜索,搜到就返回當前的index否則返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2
// 11.查詢某個Object是否在數組中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
// 對于Object數據是調用該Object.equals方法進行判斷
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false
// 12.基本數據類型數組與外包型數據類型數組互轉
ArrayUtils.toObject(new int[] { 1, 2 });// new Integer[]{Integer,Integer}
ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}
// 13.判斷數組是否為空(null和length=0的時候都為空)
ArrayUtils.isEmpty(new int[0]);// true
ArrayUtils.isEmpty(new Object[] { null });// false
// 14.合并兩個數組
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
// 15.添加一個數據到數組
ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}
// 16.刪除數組中某個位置上的數據
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 17.刪除數組中某個對象(從正序開始搜索,刪除第一個)
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}
來杭州這么久從來沒見過這么厲害的臺風,竟然可以把杭城改造成半個威尼斯.不知道為什么雨天總是給人壓抑的感覺,雖然自認為很喜歡雨天,但是卻無法將撲滅心中沮喪的心情,很想一笑而過,可惜往往無功而返.有時候很想問問自己,你真的了解鏡子里那個人么?或者說你想去了解嗎?
現在網絡上發帖機橫行,如何在盡可能少地影響用戶體驗的同時阻止發帖機是每個網站面臨的課題.
一 常見發帖機類型及其原理
1)程序自動構建字段內容,然后通過程序自動POST到服務器.
2)通過按鍵精靈之類的模擬器模擬鍵盤和鼠標操作,以達到自動發帖的目的.
二 發帖機行為分析
發帖機主要是為了發布廣告信息,所以一般發帖機都是采用即時創建帳號==>然后發帖==>然后閃人流程散布信息.
三 解決方案
1)通過增加一個特殊字段,系統可以通過驗證這個特殊字段來確認當前信息是否是系統實時產生.這種方法可以有效抑制通過程序自動POST數據的發帖機,但是無法阻止模擬器類型的發帖機.
2)驗證碼.驗證碼是最有效的阻止發帖機的手段,但驗證碼也影響了用戶的發帖體驗,根據發帖機的行為分析我們可以采用如果當前用戶注冊還未滿一個月或者發帖數還未達到10之前必需輸入驗證碼,這樣既不影響老用戶的發帖體驗又可以達到抑制發帖機的作用.
四 特殊字段產生策略
可以采用時間戳+時間戳加密(
加密方案參見我的另一篇文章),然后在服務器驗證當前客戶端傳上來的時間戳是否在允許的timeout之內.