1. 程式人生 > 其它 >python爬蟲中或者字串中或者js檔案中遇到“\x”的解決方法,轉碼方法

python爬蟲中或者字串中或者js檔案中遇到“\x”的解決方法,轉碼方法

Beanshell

Beanshell 是一種輕量級的 Java 指令碼,純 Java 編寫的,能夠動態的執行標準 java 語法及一些擴充套件指令碼語法,類似於 javaScript 和 perl。

內建變數

Beanshell Sampler 中除了可以使用標準 java 語法之外,還有一些定義好的變數,可以直接使用。

log:用來記錄日誌檔案,寫入到jmeber.log檔案,使用方法:log.info(“This is log info!”);

SampleResult:獲取SampleResult物件,可以通過這個物件獲取想要的資訊

ResponseCode:返回介面code 成功是200

ResponseMessage:獲取msg 成功是OK

vars- (JMeterVariables):用於存取 jmeter 區域性變數,很常用,一定要掌握;通常用於存取字串內容,也可以存取物件;

    a)vars.get(String key):從jmeter中獲得變數值

    b)vars.put(String key,String value):資料存到jmeter變數中

    c)vars.remove("keyname"):從 jmeter 變數中刪除 keyname。

props:用於存取 jmeter 全域性的靜態變數,其中的 key 和 value 均是字串形式;

  ymd = props.get("START.YMD");
  獲取屬性 START.YMD 的值(指令碼啟動日期)。

  props.put("PROP1","1234");
  把 1234 存入全域性屬性 PROP1 中。

ctx(JmeterContext):通過它來訪問context

  a) ctx.getCurrentSampler(); 獲取當前 sampler 請求

  b) ctx.getPreviousSampler(); 獲取前一個 sampler 請求

  c) ctx.getThreadNum(); 獲取當前執行緒的序號,從 0 開始計數

  d) ctx.getThread(); 獲取當前執行緒

  e) ctx.getThreadGroup(); 獲取當前執行緒組

  f) ctx.getProperties(); 獲取所有屬性

  g) ctx.getVariables(); 獲取當前執行緒的所有變數

Beanshell斷言

BeanShell斷言可以使用beanshell指令碼來執行斷言檢查,可以用於更復雜的個性化需求,使用更靈活,功能更強大,但是要能夠熟練使用beanshell指令碼。

在這裡除了可以使用beanshell的內建變數外,主要通過 Failure 和 FailureMessage來設定斷言結果。

if ("200".equals(""+ResponseCode) == false )
{
    // 響應碼不等於200時,設定斷言失敗,並輸出失敗資訊
    Failure=true ;
    FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ;
    print ( "the return code is " + ResponseCode);   // this goes to stdout
    log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file
} else {
    // 響應碼等於200時,設定斷言成功,並輸出成功資訊
    Failure=false;
    FailureMessage = "Return true, and the response code was " + ResponseCode;
     }
}