2016年8月29日 星期一

Python request() 亂碼

參考至:
http://sh3ll.me/2014/06/18/python-requests-encoding/

1.當其他網頁用request()都可以正常印出中文
但卻有些特定網頁會出現亂碼????

那是因為:requests 是通過http header 猜測頁面編碼,如果header中不存在charset,就會認為編碼為ISO-8859-1,所以如果返回的內容中,沒有在指定頁面編碼的話,就會出現亂碼

2.舉例
- 因為request()找不到編碼,所以亂猜出現錯誤



















3.解決方法
- If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set r.encoding appropriately before accessing this property.
在訪問 res.text屬性前先設置下正確的編碼:

res.encoding = 'utf-8'

如下圖:


Html rowspan 欄位合併

1.注意:
- tr 是一欄(行)
- 使用rowspan合併後,『第一筆』的資料需要『放在有用rowsapn的 tr 之中』
- 因為使用rowsapn,其實是『把格子往後推』

2.舉例:
- "56"用了rowsapn="3",他會把格子往後『推三格』
- 因此需要將『第一筆的資料』塞進去
- 所以也放在同一個 tr 之內

<tr>
<td rowspan="3">2</td>
<td rowspan="3">2</td>
        <td rowspan="3">[新聞]高雄亞灣變身3部曲邁向CBD</td>
        <td rowspan="3">56</td>

        <td>主文 / 共41則</td>
        <td>Ptt > Gossiping</td>
        <td>台灣新生報</td>
</tr>



3.完整範例


  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="cssName.css">
  6. <script src=""></script>
  7. <title></title>
  8. </head>
  9. <body>
  10. <table border="1px solid">
  11. <thead>
  12. <tr>
  13. <th>排序</th>
  14. <th>合併後則數</th>
  15. <th>標題</th>
  16. <th>當日回文數</th>
  17. <th>回應數</th>
  18. <th>來源</th>
  19. <th>作者</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td>1</td>
  25. <td>1</td>
  26. <td>20160822『中古屋價買新屋?建商讓利搶市、成交量恐暴衝?!』</td>
  27. <td>100</td>
  28. <td>主文 / 共100則</td>
  29. <td>Youtube關鍵字搜尋 > 關鍵字搜尋</td>
  30. <td>帥過頭房地產投資</td>
  31. </tr>
  32. <tr>
  33. <td rowspan="3">2</td>
  34. <td rowspan="3">2</td>
  35. <td rowspan="3">[新聞]高雄亞灣變身3部曲邁向CBD</td>
  36. <td rowspan="3">56</td>
  37. <td>主文 / 共41則</td>
  38. <td>Ptt > Gossiping</td>
  39. <td>台灣新生報</td>
  40. </tr>
  41. <tr>
  42. <td>主文 / 共15則</td>
  43. <td>Ptt > Kaohsiung</td>
  44. <td>【記者吳國志/</td>
  45. </tr>
  46. <tr>
  47. <td>主文 / 共15則</td>
  48. <td>Ptt > Kaohsiung</td>
  49. <td>【記者吳國志/</td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. </body>
  54.  
  55. </html>

2016年8月28日 星期日

Windows 排程器 執行Python(.py)檔 & MongoDB連線

Development Environment:
- Anaconda 3 (python3)
- mondoDB 3.2.9
- Windows7

1.產出.py檔
- 注意:自行改『副檔名』會出錯
存檔時設定為.py檔

















2.設定python排程器
-不登入也執行
-最高權限
確保排程能夠順利進行

















-設定執行的週期



















-程式與指令碼:
要執行的程式(這邊是開啟Anacoda3的python編譯器)

(Windows的 .bat 檔也是放這邊)
-新增引數:
要執行的.py檔案





















1.設定mongoDB排程器
-前面步驟與pyhon相同
-程式與指令碼:要執行的程式(這邊是開啟mongoDB的連線)
-新增引數:
因為本機不是使用『預設的db path路徑
所以在進行連線時,需要同時給予『新的db path路徑』,否則會無法執行
舉例:
mongodb:D:\mongodb\bin

預設:D:\data\db
本機:D:\mongodb\data\db

























2.測試
因為排程器開啟的mongodb連線是在背景運行
所以在cmd執行看看

- cd到mongodb\bin資料夾
- mongo.exe

出現mongodb編譯符號即成功

2016年8月16日 星期二

Constructor 建構子 - 初始化應用

1.ConstructorTest.java

有設定『建構子』的class,當該物件被建立(初始化時),建構子(某種特殊的方法)會被自動執行
因此某些『需要初始化的設定』可以寫在建構子內











2.ConstructorTestMain.java
(補充:可以把此main方法寫在 ConstructorTest.java 裡面也行,但記得要設成『靜態方法』同一個類別中,才能直接取用)












3.Result



2016年8月9日 星期二

jQuery attr() prop() 區別

參考來源:
1. http://blog.webgolds.com/view/193
2. http://wenzhixin.net.cn/2013/05/24/jquery_attr_prop

1.attribute 跟 property的區別

- property是DOM中的屬性,『是JavaScript裡的對象
- attribute是HTML標籤上的屬性,『它的值只能夠是字符串


一個例子:
<input name="sex" value="男">  

那可以代表著:<input>這個標籤擁有2個屬性:
name="sex"
value="男"
屬性值為『"sex"』&『"男"』
上述兩者即為html標籤上的屬性

第二個例子:
<input type="checkbox" checked=true/>

現在我們透過jquery來測試以上這行,得到以下結果:
語法回傳結果
$('input').prop('checked');true 
$('input').attr('checked');"checked"
看到了不同點了嗎?
布林值(DOM屬性)比『字串值(Html標籤屬性)


第三個例子:
<input type="checkbox" />

現在我們透過jquery來測試以上這行,得到以下結果:
語法回傳結果
$('input').prop('checked');false
$('input').attr('checked');undefined
例子中的
1.<input> 沒有設置 checked 
屬性當然也就是輸出 undefined.

2.prop() 獲取的到是對於一 DOM 對象,本身即具有原生的 checked 屬性當然就是輸出 false.


2.效能:.prop() > .attr(),
因為attr()需要訪問 DOM 屬性節點,比較耗時。

3.結論:
當需要獲得/設置checked 、selected、readonly 、disabled 這些DOM屬性時使用 .prop()。 


具有 true 和 false 兩個屬性的屬性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr(),具體見下表:



jQuery Ajax Practice

Note:
- 實做Ajax中的 load()、.get()、.post()、ajax()方法
- 運作方式:
- 根據不同按鈕,觸發不同的ajax事件,回傳不同的資料,並將回傳的資料塞入指定的div中

1.Html file

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  7. <title>ajax</title>
  8. <script>
  9. $(document).ready(function(){
  10. $("#button1").click(function(){
  11. $("#result").load("testHtml.html");
  12. });
  13. $("#button2").click(function(){
  14. $("#result").load("test.txt", function(responseTxt, statusTxt, xhr){
  15. if(statusTxt == "success")
  16. alert("External content loaded successfully!");
  17. if(statusTxt == "error")
  18. alert("Error: " + xhr.status + ": " + xhr.statusText);
  19. });
  20. });
  21. $("#button3").click(function(){
  22. $.get('testHtml.html', {name:"ian",age:"18"},function(data,status){alert(status)});
  23. alert("haha");
  24. });
  25. $("#button4").click(function(){
  26. $.post('testHtml.html', {name:"ian",age:"18"},function(data,status){alert(status)});
  27. alert("haha2");
  28. });
  29. $("#button5").click(function(){
  30. $.ajax({url:"testHtml.html", success:function(result){
  31. $("#result").html(result);
  32. }});
  33. });
  34. });
  35. </script>
  36. </head>
  37. <body>
  38. <button id="button1">ajax</button>
  39. <button id="button2">ajax2</button>
  40. <button id="button3">ajax3</button>
  41. <button id="button4">ajax4</button>
  42. <button id="button5">ajax5</button>
  43. //原畫面的div
  44. <div style="border:2px red solid;width:200px">
  45. </div>
  46. <div id="result">
  47. </div>
  48. </body>
  49. </html>

2.test.txt

  1. <h2>jQuery and AJAX is FUN!!!</h2>
  2.  
  3. <p id="p1">This is some text in a paragraph.</p>

3.testHtml.html

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  7. <title>aaa</title>
  8. </head>
  9. <body>
  10. <h2>Hello my name is ajax</h2>
  11. <div style="border:2px blue solid;width:200px">
  12. aa
  13. </div>
  14. </body>
  15. </html>

2016年8月5日 星期五

Spring 的威力

1.使用:
- 只要加了下面這行,就可以省去寫servlet,指定要把結果導向到哪裡?? 
@RequestMapping(value="/CompetitionStorm",method={RequestMethod.POST,RequestMethod.GET})


2.限制:
- 只會對『緊接著』的『第一個』Class做反應

@RequestMapping(value="/CompetitionStorm",method=RequestMethod.POST,RequestMethod.GET})
Public class First(){
Sout("這邊會顯示")
}

Public class Two(){
sout("這邊不會顯示")
}

3.關於設定

- 在web.xml加上下面這段

  1. <servlet>
  2. <servlet-name>mvc-dispatcher</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6.  
  7. <servlet-mapping>
  8. <servlet-name>mvc-dispatcher</servlet-name>
  9. <url-pattern>/</url-pattern>
  10. </servlet-mapping>

2016年8月4日 星期四

Generies 泛型

簡易的泛型範例

-泛型參數,不能放『八大基本型態』

-Java 7.0之後允許ClassA<foo> gf = new ClassA<>();

-<T extends Parent> 設定泛型的上限
  表示傳入的類別參數,必為Parent類別或其子類別,其他類別不能亂入


-靜態方法不能是泛型,因為靜態方法不用new


1.Gen.java



































2.GenDemo























3.Result
















4.泛型方法

2016年8月2日 星期二

Java - enum 類別

1.何謂enum?
enum定義的xxxx實際上是個類別

2.為何要有enum?
在interface中,可以定義常數。例如:
  1. public interface Action {
  2. public static final int STOP = 0;
  3. public static final int RIGHT = 1;
  4. public static final int LEFT = 2;
  5. public static final int UP = 3;
  6. public static final int DOWN = 4;
  7. }
但後面在取用時只能透過Action.STOP這種方式取,但其實不易讀 之後發展出enum,使用起來更為方便

3.實際應用

例子1

  1. public class test {
  2.  
  3. //設定建構子
  4. public enum Level {
  5. DOWNLOAD_QUANTITY(0, "AA"),
  6. DOWNLOAD_CONTENT(1, "BB"),
  7. KEYWORD_LENGTH(2, "CC"),
  8. STANDARD_PLATFORM_LOGIN(3, "登入"),
  9. EDUCATION_VIEW(4, "DD");
  10.  
  11. private int mode;
  12. private String Name;
  13.  
  14. private Level(int mode, String Name) {
  15. this.mode = mode;
  16. this.Name = Name;
  17. }
  18. }
  19.  
  20. //執行程式(提示:Level.STANDARD_PLATFORM_LOGIN 等於把STANDARD_PLATFORM_LOGIN常數的值塞入建構子內)
  21. public static void main(String[] args) {
  22. int n = Level.STANDARD_PLATFORM_LOGIN.mode;
  23. String s = Level.STANDARD_PLATFORM_LOGIN.Name;
  24. System.out.println(n); //3
  25. System.out.println(s); //登入
  26. }
  27.  
  28. }
例子2
  1.